PowerShell – aliasy

16 maja 2020 Wyłączono przez Adam [zicherka] Nogły

Ustawimy teraz aliasy komend w PowerShell’u.

[1] Zobaczmy najpierw aktualne aliasy

Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\Administrator> Get-Alias

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           % -> ForEach-Object
Alias           ? -> Where-Object
Alias           ac -> Add-Content
Alias           asnp -> Add-PSSnapin
Alias           cat -> Get-Content
Alias           cd -> Set-Location
Alias           CFS -> ConvertFrom-String                          3.1.0.0    Microsoft.PowerShell.Utility
Alias           chdir -> Set-Location
...
...
Alias           type -> Get-Content
Alias           wget -> Invoke-WebRequest
Alias           where -> Where-Object
Alias           wjb -> Wait-Job
Alias           write -> Write-Output


PS C:\Users\Administrator>
[2] Ustawmy teraz nasz nowy, własny alias.
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

# przykładowo ustawmy alias [ll] dla [Get-ChildItem]
PS C:\Users\Administrator> Set-Alias ll Get-ChildItem
PS C:\Users\Administrator> Get-Alias ll

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ll -> Get-ChildItem


PS C:\Users\Administrator> ll


    Directory: C:\Users\Administrator


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---       16.05.2020     19:44                Contacts
d-r---       16.05.2020     19:44                Desktop
d-r---       16.05.2020     19:47                Documents
d-r---       16.05.2020     19:44                Downloads
d-r---       16.05.2020     19:44                Favorites
d-r---       16.05.2020     19:44                Links
d-r---       16.05.2020     19:44                Music
d-r---       16.05.2020     19:44                Pictures
d-r---       16.05.2020     19:44                Saved Games
d-r---       16.05.2020     19:44                Searches
d-r---       16.05.2020     19:44                Videos


PS C:\Users\Administrator>
[4] Aktualnie ustawiony alias jest tymczasowy – zniknie on po zamknięciu PowerShell’a. Jeśli chcesz ustawić jakiś alias na stałe, musisz go ustawić w Twoim profilu PowerShell’a. Przekażmy zatem znaki [ll]  do naszego profilu. Odnośnie ustawiania i konfiguracji profilu odsyłam tutaj.
echo 'Set-Alias ll Get-ChildItem' > $profile
[5] Kasowanie aliasu.
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\Administrator> ll


    Directory: C:\Users\Administrator


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---       16.05.2020     19:44                Contacts
d-r---       16.05.2020     19:44                Desktop
d-r---       16.05.2020     19:47                Documents
d-r---       16.05.2020     19:44                Downloads
d-r---       16.05.2020     19:44                Favorites
d-r---       16.05.2020     19:44                Links
d-r---       16.05.2020     19:44                Music
d-r---       16.05.2020     19:44                Pictures
d-r---       16.05.2020     19:44                Saved Games
d-r---       16.05.2020     19:44                Searches
d-r---       16.05.2020     19:44                Videos


PS C:\Users\Administrator> Remove-Item alias:ll
PS C:\Users\Administrator> Get-Alias ll
Get-Alias : This command cannot find a matching alias because an alias with the name 'll' does not exist.
At line:1 char:1
+ Get-Alias ll
+ ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (ll:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

PS C:\Users\Administrator>