Skip to content

Instantly share code, notes, and snippets.

@milleniumbug
Last active March 5, 2017 16:44
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 milleniumbug/8fb0e686e1c06a0a65b1c64d3044cf67 to your computer and use it in GitHub Desktop.
Save milleniumbug/8fb0e686e1c06a0a65b1c64d3044cf67 to your computer and use it in GitHub Desktop.
for Unix converts

Note: PowerShell is object based, as opposed to byte stream based Unix utilities. This means text operations aren't that common, and also means many idioms aren't directly translateable.

Equivalents of common commands and idioms

  • var=text -> $var = object
  • cat file1 file2 file3 > target -> gc -Encoding Byte file1,file2,file3 | sc -Encoding Byte target (binary concatenation, with text files you can specify different encoding or let it guess)
  • curl -> iwr (Invoke-WebRequest)
  • date -> date (Get-Date)
  • man command-name -> help command-name (Get-Help)
  • ps -> ps (Get-Process)
  • seq 10 -> 1..10
  • sort -> sort (Sort-Object)
  • uniq -> gu (Get-Unique)
  • unset var -. rv var (Remove-Variable)

.NET interop

Create a new object:

# equivalent to C#'s var d = new DateTime(2015, 3, 1);
2015, 3, 1 is an array here
$d = New-Object DateTime 2015, 3, 1

Create an object of a generic class:

# equivalent to C#'s var l = new SortedList<string, int>();
$l = New-Object 'Collections.Generic.SortedList[string, int]'

Call a method:

$d = $d.AddHours(4)

Access a property:

$month = $d.Month
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment