In PowerShell, there are many ways to achieve the same result. This can be illustrated nicely with the simple & familiar Hello World
example:
Using Write-Host
:
Write-Host "Hello World"
Using Write-Output
:
Write-Output 'Hello world'
It's worth noting that although Write-Output
& Write-Host
both write to the screen there is a subtle difference. Write-Host
writes only to stdout (i.e. the console screen), whereas Write-Output
writes to both stdout AND to the output [success] stream allowing for redirection. Redirection (and streams in general) allow for the output of one command to be directed as input to another including assignment to a variable.
> $message = Write-Output "Hello World"
> $message
"Hello World"
These similar functions are not aliases, but can produce the same results if one wants to avoid "polluting" the success stream.
Write-Output
is aliased to Echo
or Write
Echo 'Hello world'
Write 'Hello world'
Or, by simply typing 'Hello world'!
'Hello world'
All of which will result with the expected console output
Hello world
Another example of aliases in PowerShell is the common mapping of both older command prompt commands and BASH commands to PowerShell cmdlets. All of the following produce a directory listing of the current directory.
C:\Windows> dir
C:\Windows> ls
C:\Windows> Get-ChildItem
Finally, you can create your own alias with the Set-Alias cmdlet! As an example let's alisas Test-NetConnection
, which is essentially the PowerShell equivalent to the command prompt's ping command, to "ping".
Set-Alias -Name ping -Value Test-NetConnection
Now you can use ping
instead of Test-NetConnection
! Be aware that if the alias is already in use, you'll overwrite the association.
The Alias will be alive, till the session is active. Once you close the session and try to run the alias which you have created in your last session, it will not work. To overcome this issue, you can import all your aliases from an excel into your session once, before starting your work.