Try Documentalist,
my app that offers fast, offline access to 190+ programmer API docs.
variables
# Variables
# param defines -sha script argument and sets "" as default value
$args, $args[0] : args passed to script, by position
if ($sha -eq "") {
$sha = (git rev-parse HEAD) | Out-String
$sha = $sha.Trim()
}
# Variable expansion
Write-Host "sha: $sha" # writes the value of $sha
Write-Host 'sha: $sha is not expanded' # $sha is literal, not expanded inside ''
useful functions
Write-Host
Out-String
Get-Variable
: list all PowerShell variablesGet-ChildItem env:
: list all env variablesgci env:PATH
: get $PATHGet-ChildItem -Recurse -Filter "*.chm" -EA SilentlyContinue | Select Length,Name
: find all *.chm fileswget "http://blog.kowalczyk.info" -OutFile "index.html"
: download url to index.html.wget
is alias forInvoke-WebRequest
get-command code
: equivalent ofwhich
in unix, find full path of vscode executable
# get PowerShell to inherit changes from .bat file:
cmd.exe /k "scripts\vc.bat & powershell"
path
# $path is dict Name/Value, where Name is "Path" and Value is value of PATH environment variable
$path = gci env:PATH
$dirs = $path.Value.Split(";")
(gci env:Path).Value.Split(";")
strings
$line.Split("{.}")[1]
: split $line by "." and get the second part
$env:PATH -split ';'
: show components of $PATH
$line -Like "*.xml"
: return true if matches regular expression
[IO.Path]::GetExtension($line)
: call a function on $line
functions, defining and calling
function isFileOnList($list, $file) {
foreach ($item in $list) {
$inList = $item.ToLower().EndsWith($file)
if ($inList) {
return $inList
}
}
return False
}
$list = @("foo\bar.cpp", "foo.txt")
$in = isFileOnList($list, ".txt")
booleans: $true/$false
sorting
gci | Sort-Object Name -Descending
: sort list of files by Name
in descending order
call external programs
# execute 'git rev-parse HEAD' and assign to a variable
$sha1 = (git rev-parse HEAD) | Out-String
$sha1 = $sha1.Replace([System.Environment]::NewLine,"")
comments
# single-line comment
<#
multi-line
comment
#>
stop script on errors
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# call exitIfFailed after executing a command
function exitIfFailed { if ($LASTEXITCODE -ne 0) { exit } }
dates
$DATE = [int][double]::Parse((Get-Date -UFormat %s))
Notes:
- initial setup:
Set-ExecutionPolicy Unrestricted
(as admin) - https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-member?view=powershell-5.1 : list of functions
- Before Windows 10:
- PowerShell before v5: install PackageManagement PowerShell Modules from https://www.microsoft.com/en-us/download/details.aspx?id=49186
Install-Module PSReadline
to install PSReadLine
Execute .exe with spaces in path:
& "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\editbin.exe" /stack:1048576 FindFilesGo.exe
Links:
- https://github.com/StackExchange/dnscontrol/blob/master/build.ps1
- https://github.com/golang/build/blob/master/env/windows/startup.ps1
- http://www.hanselman.com/blog/PromptsAndDirectoriesEvenBetterGitAndMercurialWithPowerShell.aspx
- https://gist.github.com/MichaelPote/92fa6e65eacf26219022, https://www.reddit.com/r/programming/comments/441do9/i_made_a_windows_powershell_script_that_puts_a/
- https://github.com/ljw1004/pdb2github/blob/master/github-sourceindexer.ps1