{"id":7247,"date":"2020-12-09T16:50:23","date_gmt":"2020-12-09T15:50:23","guid":{"rendered":"https:\/\/blog.via-internet.de\/?p=7247"},"modified":"2020-12-09T16:50:23","modified_gmt":"2020-12-09T15:50:23","slug":"power-shell-working-with-aliases","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2020\/12\/09\/power-shell-working-with-aliases\/","title":{"rendered":"Power Shell | Working with Aliases"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Related CommandLets<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><code>Get-Alias<\/code><\/li><li><code>New-Alias<\/code><\/li><li><code>Set-Alias<\/code><\/li><li><code>Export-Alias<\/code><\/li><li><code>Import-Alias<\/code>&nbsp;<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Display Aliases<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Show Aliases<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Alias<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Show Aliases for a Command<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Alias -Definition Get-ChildItem<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.via-internet.de\/wp-content\/uploads\/2020\/12\/image-12-700x85.png\" alt=\"\" class=\"wp-image-7248\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Export the entire list to a spreadsheet (a CSV file) by using the Export-Alias:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Export-Alias -Path \"Alias.csv\"<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Show only build-in aliases (with property read-only)<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Alias | Where-Object {$_.Options -Match \"ReadOnly\"}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Create Alias<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">New-Alias -Name D -Value Get-ChildItem -Description \"A shortcut to the Get-ChildItem cmdlet\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create a shortcut &#8216;c&#8217; for Visual Studio Code &#8216;code&#8217;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Set-Alias -Name c -Value code  <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create more complex aliases using functions<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"8\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Start-TheDay {\n    start chrome;\n    start firefox;\n    start code;\n    start slack;\n}\n\nSet-Alias -Name am -Value Start-TheDay  <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Customize prompt<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_prompts?view=powershell-7.1\">Microsoft Powershell Core &#8211; About Prompts<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Prompt(){\n   $W = Split-Path -leaf -path (Get-Location)\n   $prompt = Write-Prompt \"$($env:UserName)@$($env:ComputerName):\" -ForegroundColor Green\n   $prompt += Write-Prompt $W -ForegroundColor DarkCyan\n   $prompt += Write-Prompt '>'\n   \n   return ' '\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Get code that sets the prompt<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(Get-Command Prompt).ScriptBlock<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Samples<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function prompt {\"PS [$env:COMPUTERNAME]&amp;gt; \"}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function prompt {\"$(Get-Date)> \"}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function prompt {\n  $identity = [Security.Principal.WindowsIdentity]::GetCurrent()\n  $principal = [Security.Principal.WindowsPrincipal] $identity\n  $adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator\n\n  $(if (Test-Path variable:\/PSDebugContext) { '[DBG]: ' }\n    elseif($principal.IsInRole($adminRole)) { \"[ADMIN]: \" }\n    else { '' }\n  ) + 'PS ' + $(Get-Location) +\n    $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function prompt {\n   # The at sign creates an array in case only one history item exists.\n   $history = @(Get-History)\n   if($history.Count -gt 0)\n   {\n      $lastItem = $history[$history.Count - 1]\n      $lastId = $lastItem.Id\n   }\n\n   $nextCommand = $lastId + 1\n   $currentDirectory = Get-Location\n   \"PS: $nextCommand $currentDirectory >\"\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function prompt {\n    $color = Get-Random -Min 1 -Max 16\n    Write-Host (\"PS \" + $(Get-Location) +\">\") -NoNewLine `\n     -ForegroundColor $Color\n    return \" \"\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Prompt\n{\n     # Admin ?\n     if( (\n        New-Object Security.Principal.WindowsPrincipal (\n            [Security.Principal.WindowsIdentity]::GetCurrent())\n        ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))\n    {\n        # Admin-mark in WindowTitle\n        $Host.UI.RawUI.WindowTitle = \"[Admin] \" + $Host.UI.RawUI.WindowTitlep\n        # Admin-mark on prompt\n        Write-Host \"[\" -nonewline -foregroundcolor DarkGray\n        Write-Host \"Admin\" -nonewline -foregroundcolor Red\n        Write-Host \"] \" -nonewline -foregroundcolor DarkGray\n    }   \n    Write-Host \"[\" -NoNewline\n    Write-Host (Get-Date -Format \"HH:mm:ss\") -ForegroundColor Gray -NoNewline\n    Write-Host \"] [\" -NoNewline    \n    Write-Host \"$(((H)[-1].EndExecutionTime - (H)[-1].StartExecutionTime).Milliseconds) ms\" -NoNewline -ForegroundColor Gray\n    Write-Host \"]\" -NoNewline\n    if(Get-Module Posh-git) {Write-VcsStatus; Write-host ''}\n    Write-Host \"$($executionContext.SessionState.Path.CurrentLocation.ProviderPath)\" -NoNewline\n    \"> \"\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Docker Aliases<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dockerpowershellalias1drm\"><code>drm<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Removes all stopped containers:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Remove-StoppedContainers {  \n    docker container rm $(docker container ls -q)\n}\nSet-Alias drm  Remove-StoppedContainers  \n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dockerpowershellalias2drmf\"><code>drmf<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Removes&nbsp;<strong>all<\/strong>&nbsp;containers, whether they&#8217;re running or not:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Remove-AllContainers {  \n    docker container rm -f $(docker container ls -aq)\n}\nSet-Alias drmf  Remove-AllContainers  \n<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Use with caution<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dockerpowershellalias3dip\"><code>dip<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Gets the container&#8217;s IP address &#8211; pass it a container name or part of the container ID, e.g.&nbsp;<code>dip 02a<\/code>&nbsp;or&nbsp;<code>dip dbserver<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Get-ContainerIPAddress {  \n    param (\n        [string] $id\n    )\n    &amp; docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' $id\n}\nSet-Alias dip  Get-ContainerIPAddress  \n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dockerpowershellalias4d2h\"><code>d2h<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Adds a container&#8217;s IP address to the host&#8217;s&nbsp;<code>hosts<\/code>&nbsp;file, so you can refer to containers by their name on your Docker host, in the same way that containers reach each other by name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example &#8211; I have a web app which uses a SQL database. In dev and test environments I&#8217;ll be running SQL Server in a container for the database. The container is called&nbsp;<code>petshop-db<\/code>&nbsp;and all the connection strings in the web configuration use&nbsp;<code>petshop-db<\/code>&nbsp;as the database server name. If I want to run the web app locally, but still use a container for the database I just start the container and run&nbsp;<code>d2h petshop-db<\/code>. Now my web app uses the container IP from the hosts file, and I can run the whole stack with&nbsp;<code>docker-compose up<\/code>&nbsp;without changing config.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Add-ContainerIpToHosts {  \n    param (\n        [string] $name\n    )\n    $ip = docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' $name\n    $newEntry = \"$ip  $name  #added by d2h# `r`n\"\n    $path = 'C:WindowsSystem32driversetchosts'\n    $newEntry + (Get-Content $path -Raw) | Set-Content $path\n}\nSet-Alias d2h  Add-ContainerIpToHosts  <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Network Aliases<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">List Jobs for Port<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function List-Jobs-Using-Port {  \n    param (\n        [string] $port\n    )\n\n    Get-Process -Id (Get-NetTCPConnection -LocalPort $port).OwningProcess\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Top Ten Aliases<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/\" target=\"_blank\" rel=\"noreferrer noopener\">Guy&#8217;s Top Ten Aliases<\/a><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Cd:_Set-Location\">Cd: Set-Location<\/a><\/li><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Cls:_Clear-Host\">Cls: Clear-Host<\/a><\/li><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Del:_Remove-Item\">Del: Remove-Item<\/a><\/li><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Diff:_Compare-Object\">Diff: Compare-Object<\/a><\/li><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Dir:_Get-ChildItem\">Dir: Get-ChildItem<\/a><\/li><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Kill:_Stop-Process_\">Kill: Stop-Process<\/a><\/li><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Echo:_Write-Output_\">Echo: Write-Output<\/a><\/li><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Sort:_Sort-Object\">Sort: Sort-Object<\/a><\/li><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Sleep:_Start-Sleep\">Sleep: Start-Sleep<\/a><\/li><li><a href=\"https:\/\/www.computerperformance.co.uk\/powershell\/alias-top10\/#Type:_Get-Content\">Type: Get-Content<\/a><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">List of Aliases<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Name<\/td><td class=\"has-text-align-left\" data-align=\"left\">Definition<\/td><\/tr><tr><td>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">More to read<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/stackify.com\/powershell-commands-every-developer-should-know\/\">https:\/\/stackify.com\/powershell-commands-every-developer-should-know\/<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References and Copyright<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Aliases and Prompts<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/gist.github.com\/sixeyed\/c3ae1fd8033b8208ad29458a56856e05\">https:\/\/gist.github.com\/sixeyed\/c3ae1fd8033b8208ad29458a56856e05<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Related CommandLets Get-Alias New-Alias Set-Alias Export-Alias Import-Alias&nbsp; Display Aliases Show Aliases Show Aliases for a Command Export the entire list to a spreadsheet (a CSV file) by using the Export-Alias: Show only build-in aliases (with property read-only) Create Alias Create a shortcut &#8216;c&#8217; for Visual Studio Code &#8216;code&#8217; Create more complex aliases using functions Customize prompt Microsoft Powershell Core &#8211; About Prompts Get code that sets the prompt Samples Docker Aliases drm Removes all stopped containers: drmf Removes&nbsp;all&nbsp;containers, whether they&#8217;re running or not: Use with caution dip Gets the container&#8217;s IP address &#8211; pass it a container name or part of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7170,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[102],"tags":[],"class_list":["post-7247","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-power-shell"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/7247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=7247"}],"version-history":[{"count":0,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/7247\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=7247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=7247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=7247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}