Skip to content

Instantly share code, notes, and snippets.

@rpalo
Last active April 7, 2018 15:15
Show Gist options
  • Save rpalo/d9f980613c818c8b0ede4815bf1ba1a1 to your computer and use it in GitHub Desktop.
Save rpalo/d9f980613c818c8b0ede4815bf1ba1a1 to your computer and use it in GitHub Desktop.
Code Samples for Intro to PowerShell Article
PS> Get-ChildItem env:
# And to get to a specific one:
PS> Get-Content env:PATH
PS> $env:PATH
PS> "2" + 2
22 # A string
PS> [Int] "2" + 2
4 # An integer. The conversion only applies to the "2"
PS> Get-Command Get-Alias | Format-List
PS> Get-Command
PS> Get-Command Get-Help | Format-List
# Or, if you're feeling cheeky:
PS> Get-Help Get-Help
PS> Get-Help command
# You can also get help by adding the ? parameter
PS> command -?
PS> Get-Command -noun Job
PS> Get-ChildItem
# You can also do:
PS> gci
PS> dir
# And, just to make you feel a little more at home...
PS> ls
PS> Get-ChildItem
PS> Get-Process | Get-Member
# Another similar command that also prints out the values is:
PS> (Get-Process)[0] | Format-List
PS> Get-Command -verb new
PS> (Get-Process)[0] | Get-TypeData
PS> Test-Path $profile
# Use whichever editor you love best
PS> code $profile
# Microsoft.PowerShell_profile.ps1
# You can customize the window itself by accessing $Host.UI.RawUI
$window = $Host.UI.RawUI
$window.WindowTitle = "Pa-pa-pa-pa-pa-pa-POWERSHELL"
$window.BackgroundColor = "Black"
$window.ForegroundColor = "Gray"
# You can define functions (remember to follow the Verb-Noun convention!)
function Count-History {
(Get-History | Measure-Object).count
}
function beep {
echo `a
}
function Edit-Profile {
[CmdletBinding()]
[Alias("ep")]
PARAM()
vim $profile
}
# You can set aliases
# NOTE: In PowerShell you can only alias simple commands. Unlike Bash,
# you can't alias commands with arguments flags. If you want to do that
# you should define a function instead.
Set-Alias touch New-Item # old habits die hard, amirite?
# You can customize your prompt!
function prompt {
# ... see the next section for more details on this.
}
PS> $profile | Get-Member -type NoteProperty
PS> $profile
~/Documents/blog
PS [102] master ->
function prompt {
$loc = (Get-Location).Path.Replace("$HOME", "~")
$gitBranch = git branch | Select-String "\*"
if (!$gitBranch) {
$gitBranch = ""
} else {
$gitBranch = $gitBranch.ToString().Replace("`* ", "")
}
$histCount = (Get-History | Measure-Object).count
Write-Host -ForegroundColor yellow "`n $loc"
Write-Host -NoNewLine "PS [$histCount] $gitBranch ->"
return " "
}
function prompt {
$histCount = (Get-History | Measure-Object).count
return "POWERSHELL LEVEL OVER $histCount THOUSAND!!! >"
}
PS> Get-Process | Sort-Object CPU -descending | Select-Object -first 10
PS> Get-Process | Sort-Object CPU -descending | Select-Object ProcessName -first 10
PS> Get-Process | Select-Object Id, ProcessName -last 20
# pwd
PS> Get-Location # or gl or pwd
# ls
PS> Get-ChildItem # or gci or dir or ls
# cd
PS> Set-Location # or sl or chdir or cd
# cp
PS> Copy-Item # or copy or cpi or cp
# mv
PS> Move-Item # or move or mi or mv
# cat
PS> Get-Content # or gc or type
# mkdir
PS> New-Item -ItemType Directory # use ni for short
# touch
PS> New-Item -ItemType File # use ni for short
# rm
PS> Remove-Item # or del or erase or ri
# rm -rf
PS> Remove-Item -Recurse -Force
# head or tail
PS> Select-Object -first # or -last
# usage: Get-Process | Select-Object -first 10
# find
PS> Get-ChildItem -filter *.rb -recurse .
# but for a slightly easier to read version:
PS> Get-ChildItem -filter *.rb -recurse . | Select-Object FullName
PS> Get-Process | Where-Object WS -gt 150MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment