Home / Tools and services / PowerShell edit
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 variables
    • Get-ChildItem env: : list all env variables
    • gci env:PATH : get $PATH
    • Get-ChildItem -Recurse -Filter "*.chm" -EA SilentlyContinue | Select Length,Name : find all *.chm files
    • wget "http://blog.kowalczyk.info" -OutFile "index.html" : download url to index.html. wget is alias for Invoke-WebRequest
    • get-command code : equivalent of which 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:
Execute .exe with spaces in path: & "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\editbin.exe" /stack:1048576 FindFilesGo.exe
Links:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:

Share on        

Need fast, offline access to 190+ programmer API docs? Try my app Documentalist for Windows