Skip to content

Instantly share code, notes, and snippets.

@coin8086
Last active April 15, 2024 09:49
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 coin8086/8f79199c667cfbdcd94793d9e7875123 to your computer and use it in GitHub Desktop.
Save coin8086/8f79199c667cfbdcd94793d9e7875123 to your computer and use it in GitHub Desktop.
PowerShell vs Bash

PowerShell vs Bash

Key different points

  • Bash commands output strings while PS commands output objects!

Examples

The following standard aliases in PowerShell are used:

  • dir -> Get-ChildItem
  • sls -> Select-String
  • cat -> Get-Content
  • rm -> Remove-Item
  • cp -> Copy-Item
  • select -> Select-Object
  • ? -> Where-Object
  • % -> ForEach-Object

Search in files

Bash

find 'dir' -type f -name '*.txt' -exec grep -n 'pattern' '{}' +

PowerShell

dir 'dir\*.txt' -Recurse | sls 'pattern'

Find out files that do not contain a pattern

Bash

find 'dir' -type f -name '*.txt' ! \( -exec grep -q 'pattern' '{}' \; \) -print

PowerShell

dir 'dir\*.txt' -Recurse | ?{ !(sls -Quiet -Path $_ -Pattern 'pattern') }

Find and delete files

Bash

find 'dir' -name '*.txt' -delete

PowerShell

dir 'dir\*.txt' -Recurse | rm

View first/last n lines of a file

Bash

head 'file'
tail 'file'

PowerShell

cat 'file' -Head 10
cat 'file' -Tail 10

Or

cat 'file' | select -First 10
cat 'file' | select -Last 10

Find command

Bash

type -a 'command'

PowerShell

Get-Command -all 'command'

Grep

Bash

<command write to stdout> | grep 'pattern'

PowerShell

<command write to stdout> | oss | sls 'pattern'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment