Last active
April 7, 2018 15:15
-
-
Save rpalo/d9f980613c818c8b0ede4815bf1ba1a1 to your computer and use it in GitHub Desktop.
Code Samples for Intro to PowerShell Article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> 2 + 2 | |
4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> 4.2 / 3 | |
1.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-ChildItem env: | |
# And to get to a specific one: | |
PS> Get-Content env:PATH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> $env:PATH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Process |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> "2" + 2 | |
22 # A string | |
PS> [Int] "2" + 2 | |
4 # An integer. The conversion only applies to the "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Command Get-Alias | Format-List |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Alias |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Command Get-Help | Format-List | |
# Or, if you're feeling cheeky: | |
PS> Get-Help Get-Help |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Help command | |
# You can also get help by adding the ? parameter | |
PS> command -? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Command -noun Job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-ChildItem | |
# You can also do: | |
PS> gci | |
PS> dir | |
# And, just to make you feel a little more at home... | |
PS> ls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-ChildItem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ls -l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Process | Get-Member | |
# Another similar command that also prints out the values is: | |
PS> (Get-Process)[0] | Format-List |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Command -verb new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> (Get-Process)[0] | Get-TypeData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Test-Path $profile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use whichever editor you love best | |
PS> code $profile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> $profile | Get-Member -type NoteProperty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> $profile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/Documents/blog | |
PS [102] master -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 " " | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function prompt { | |
$histCount = (Get-History | Measure-Object).count | |
return "POWERSHELL LEVEL OVER $histCount THOUSAND!!! >" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Process | Sort-Object CPU -descending | Select-Object -first 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Process | Sort-Object CPU -descending | Select-Object ProcessName -first 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS> Get-Process | Select-Object Id, ProcessName -last 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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