Aliases similar functions

suggest change

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'

Although Write-Output and Write-Host both appear on screen, they do not write to the same stream. Write-Host writes to the host (the information stream in Windows PowerShell 5.0 and later), not to the success stream. Write-Output writes only to the success stream, which the host then displays. Success-stream output can be redirected, piped, or assigned to a variable.

$message = Write-Output "Hello World"
$message
Hello World

Write-Host and Write-Output are not aliases of each other. Use Write-Host when you want to avoid writing to 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, alias Test-NetConnection to ping. Test-NetConnection can test a connection, but it is not a drop-in replacement for ping.exe.

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 lasts only while the session is active. After you close the session, an alias created in that session is gone. To keep aliases, save them with Export-Alias and import them with Import-Alias when you start work, or define them in your profile.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents