Skip to content

Instantly share code, notes, and snippets.

@javafun
Forked from sixeyed/profile.ps1
Created May 21, 2020 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javafun/b4b6345412180ced11a6ee876bd3278a to your computer and use it in GitHub Desktop.
Save javafun/b4b6345412180ced11a6ee876bd3278a to your computer and use it in GitHub Desktop.
PowerShell profile with a Linux-style prompt and aliases for common Docker commands
function Prompt(){
$W = Split-Path -leaf -path (Get-Location)
$prompt = Write-Prompt "$($env:UserName)@$($env:ComputerName):" -ForegroundColor Green
$prompt += Write-Prompt $W -ForegroundColor DarkCyan
$prompt += Write-Prompt '>'
return ' '
}
function Remove-StoppedContainers {
docker container rm $(docker container ls -q)
}
function Remove-AllContainers {
docker container rm -f $(docker container ls -aq)
}
function Get-ContainerIPAddress {
param (
[string] $id
)
& docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' $id
}
function Add-ContainerIpToHosts {
param (
[string] $name
)
$ip = docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' $name
$newEntry = "$ip $name #added by d2h# `r`n"
$path = 'C:\Windows\System32\drivers\etc\hosts'
$newEntry + (Get-Content $path -Raw) | Set-Content $path
}
Set-Alias drm Remove-StoppedContainers
Set-Alias drmf Remove-AllContainers
Set-Alias dip Get-ContainerIPAddress
Set-Alias d2h Add-ContainerIpToHosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment