Alias in PowerShell

Содержание
Get-Alias: existing aliases
Delete alias
Create alias
Configuration
Errors
My aliases
Related articles

Get-Alias

To get a list of existing aliases, you need to run

Get-Alias

CommandType Name Version Source ----------- ---- ------- ------ Alias % -> ForEach-Object Alias ? -> Where-Object Alias ac -> Add-Content Alias asnp -> Add-PSSnapin Alias badalias -> Wrong-Command Alias cat -> Get-Content Alias cd -> Set-Location Alias CFS -> ConvertFrom-String 3.1.0.0 Microsoft.PowerShell.Utility Alias chdir -> Set-Location Alias clc -> Clear-Content Alias clear -> Clear-Host …

There are a lot of standard aliases, here you can see the beginning of the list and one incorrect alias, which we will delete here

Deleting an alias

Starting from version 6 in PowerShell, you can delete aliases with the Remove-Alias command

You can read about how to install PowerShell 7 here

Remove-Alias badalias

In earlier versions, you need to use the Remove-Item command

Remove-Item Alias:badalias

The easiest way to check for alias deletion is to use Select-String

Get-Alias | Select-String -Pattern "badalias"

Creating an alias

You can create an alias with the Set-Alias command

Set-Alias -Name reboot -Value Restart-Computer

If the alias uses a path with spaces, they can be escaped using the character `

Set-Alias -Name app -Value C:\Program` Files\App\app.exe

Configuration

In order for aliases and functions to be saved after a reboot, they need to add to the PowerShell profile settings file

Its location can be found by the command

$profile

C:\Users\Andrei\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

If $profile shows a non-existent path, then the settings file can be created manually.

New-Item -Path $profile -Force -ItemType "file"

The file can then be opened and edited in notepad

notepad $profile

Set-Alias -Name reboot -Value Restart-Computer

PowerShell needs to be restarted.

An example of an alias that runs Python , that is used by TestComplete

Set-Alias -Name tcpython -Value "C:\Program Files (x86)\SmartBear\TestComplete 15\x64\Bin\Extensions\Python\Python310\python.exe"

Errors

Remove-Alias : The term 'Remove-Alias' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + Remove-Alias somealias + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Remove-Alias:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

Remove-Alias has only been supported since the sixth version of PowerShell

Need to update PowerShell or use the Remove-Item command

Remove-Item Alias:somealias

My aliases

Set-Alias -Name firefox -Value C:\Program` Files\Mozilla` Firefox\firefox.exe Set-Alias -Name vi -Value C:\Program` Files\Vim\vim91\vim.exe Set-Alias -Name reboot -Value Restart-Computer Set-Alias -Name tcpython -Value "C:\Program Files (x86)\SmartBear\TestComplete 15\x64\Bin\Extensions\Python\Python310\python.exe"

Article author: Andrei Olegovich

Похожие статьи
Windows
PowerShell
Install
Basics
Alias
Functions
Network
Users
Files
REST API requests
Errors
Изображение баннера

Поиск по сайту

Подпишитесь на Telegram канал @aofeed чтобы следить за выходом новых статей и обновлением старых

Перейти на канал

@aofeed

Задать вопрос в Телеграм-группе

@aofeedchat

Контакты и сотрудничество:
Рекомендую наш хостинг beget.ru
Пишите на info@urn.su если Вы:
1. Хотите написать статью для нашего сайта или перевести статью на свой родной язык.
2. Хотите разместить на сайте рекламу, подходящую по тематике.
3. Реклама на моём сайте имеет максимальный уровень цензуры. Если Вы увидели рекламный блок недопустимый для просмотра детьми школьного возраста, вызывающий шок или вводящий в заблуждение - пожалуйста свяжитесь с нами по электронной почте
4. Нашли на сайте ошибку, неточности, баг и т.д. ... .......
5. Статьи можно расшарить в соцсетях, нажав на иконку сети: