Skip to content

Instantly share code, notes, and snippets.

@coin8086
Last active May 6, 2025 08:16
Show Gist options
  • 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 in Examples

PowerShell vs Bash in Examples

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'

NOTE

sls' behavior in pipeline depends on the type of input objects. Here dir produces FileInfo objects and thus sls searches in the content of the files represented by FileInfo objects, rather than the ToString() result of FileInfo objects. If the input is plain text, then sls just searches in the text and there is no magic like the FileInfo inputs.

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

View first/last n lines/objects from pipeline

Bash

... | head
... | tail

PowerShell

... | select -First 10
... | 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