Windows


Windows: Powershell

Use Powershell for command line functionality under windows.


Powershell: Alias

Add an alias to easily perform a function

PowerShell alias cmdlets

Aliases can be managed with five PowerShell cmdlets: Export-alias, Import-alias, Get-alias, Set-alias, and New-alias.

Export-alias and Import-alias simply save or load aliases to or from a text file, respectively, allowing you to back up and restore aliases. Note that PowerShell 3.0 and later does not overwrite existing aliases, by default, when you import from a file. To overwrite an existing alias, you need to use the -Force argument.

Get-alias, as already mentioned, displays existing aliases along with their meaning. You can use wildcards or exclude items to filter the display.

Finally, New-alias and Set-Alias are both very similar. The difference is subtle. New-alias will error if you try to create an alias that already exists, whereas Set-alias will happily create a brand new alias or overwrite an existing one.

Create an alias

You can create an alias for any cmdlet, script, function within a script, or even an external command. The choice is limited only by your creativity. To create your own alias, you need to use either the New-Alias or Set-Alias command. The simplest syntax is:

Set-alias

So, if you want to open Notepad by typing “ed,” you use:

Set-alias ed notepad

That’s easy but not that useful. The secret to benefiting from aliases is to use them for complex PowerShell scripts or external commands that have horrible paths too hard to type or remember.

A few aliasing issues

Aliasing has a few notable caveats to be aware of. One issue is that many of the built-in aliases are read-only.

What that means is those aliases are protected from accidental use—that is, you cannot delete them easily. You need to use –Force to delete; however, doing so is not recommended.

The second big issue is that aliases only exist for the lifetime of the shell window. This is very similar to the DOS Set command. If you create an environment variable in a DOS window and then close that window, you destroy the variable. PowerShell aliases behave in the same way. The solution is to add the alias to your PowerShell profile.

Before you add aliases to your profile, however, be selective. Choose only the commands that you use most often to be aliases. If you have too many, you will struggle to remember them all and thus defeat the whole purpose of an alias in the first place—its usability.

There is, however, one core catch to aliasing in PowerShell that even DOS betters. It’s only practical to alias single commands or cmdlets. That means you cannot alias long, multi-command strings or even add parameters. Doing so will cause the command to fail.

Creating complex aliases

The solution, ironically, is to use functions. So, to edit your profile with “edss,” you need to define a function first and then alias the function.

Function Edit-PowerShellProfile {notepad path\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1}
New-alias edal Edit-PowerShellProfile

Now that your appetite is whetted, here are some creative alias/function pairs:

Function Get-IP4 {Ipconfig | sls IPv4}
New-Alias IP Get-IP

Function Get-BootTime {wmic OS get LastBootupTime}
New-Alias GBT Get-BootTime

Function Get-Version {$psversiontable}
New-Alias ver Get-Version

Function Get-SerialNumber {(Get-WmiObject -Class:Win32_BIOS).SerialNumber}
New-Alias GSer Get-SerialNumber

Function Get-Model {(Get-WmiObject -Class:Win32_ComputerSystem).Model}
New-Alias GML Get-Model

In order of appearance, these aliases display eventlog errors per day, last boot time of a machine, PowerShell version, and, finally, two deployment values: machine serial number and machine model from the BIOS, respectively.

You can, of course, split your aliases off into a separate file altogether if you want, using Export-Alias and Import-Alias. Note that the export will overwrite the file. To prevent that, you can use the wonderfully named –noclobber option:

Export-Alias D:\YourAliases.txt –noclobber

Import-Alias D:\youraliases.txt


Powershell: Customise the color profile

Customise the color profile and get the Powershell looking how you want, add to your profile so it loads each time you use Powershell.

Dark theme

$console = $host.UI.RawUI
$console.ForegroundColor = "Gray"
$console.BackgroundColor = "Black"

Clear-Host

$colors = $host.PrivateData
$colors.VerboseForegroundColor = "DarkCyan"
$colors.VerboseBackgroundColor = "Black"
$colors.WarningForegroundColor = "Yellow"
$colors.WarningBackgroundColor = "Black"
$colors.ErrorForegroundColor = "Magenta"
$colors.ErrorBackgroundColor = "Black"
$colors.DebugForegroundColor = "Green"
$colors.DebugBackgroundColor = "Black"
$colors.ProgressForegroundColor = "Gray"
$colors.ProgressBackgroundColor = "Black"

Set-Location c:\

Clear-Host

Light theme

$console=$host.UI.RawUI
$console.ForegroundColor="black"
$console.BackgroundColor="white"

Clear-Host

$colors=$host.PrivateData
$colors.VerboseForegroundColor="white"
$colors.VerboseBackgroundColor="blue"
$colors.WarningForegroundColor="yellow"
$colors.WarningBackgroundColor="darkgreen"
$colors.ErrorForegroundColor="white"
$colors.ErrorBackgroundColor="red"
$colors.DebugForegroundColor="Green"
$colors.DebugBackgroundColor="white"
$colors.ProgressForegroundColor="Gray"
$colors.ProgressBackgroundColor="white"

Set-Location c:\

Clear-Host

Show console colors

Function Get-ConsoleColor {
Param(
[switch]$Colorize
)

$wsh = New-Object -ComObject wscript.shell

$data = [enum]::GetNames([consolecolor])

if ($Colorize) {
Foreach ($color in $data) {
Write-Host $color -ForegroundColor $Color
}
[void]$wsh.Popup("The current background color is $([console]::BackgroundColor)",16,"Get-ConsoleColor")
}
else {
#display values
$data
}

} #Get-ConsoleColor -Colorize

Test function to show theme

Function Test-ConsoleColor {
[cmdletbinding()]
Param()

Clear-Host
$heading = "White"
Write-Host "Pipeline Output" -ForegroundColor $heading
Get-Service | Select -first 5

Write-Host "`nError" -ForegroundColor $heading
Write-Error "I made a mistake"

Write-Host "`nWarning" -ForegroundColor $heading
Write-Warning "Let this be a warning to you."

Write-Host "`nVerbose" -ForegroundColor $heading
$VerbosePreference = "Continue"
Write-Verbose "I have a lot to say."
$VerbosePreference = "SilentlyContinue"

Write-Host "`nDebug" -ForegroundColor $heading
$DebugPreference = "Continue"
Write-Debug "`nSomething is bugging me. Figure it out."
$DebugPreference = "SilentlyContinue"

Write-Host "`nProgress" -ForegroundColor $heading
1..10 | foreach -Begin {$i=0} -process {
$i++
$p = ($i/10)*100
Write-Progress -Activity "Progress Test" -Status "Working" -CurrentOperation $_ -PercentComplete $p
Start-Sleep -Milliseconds 250
}
} #Test-ConsoleColor