Vous êtes sur la page 1sur 5

3/21/2018 PowerShell vs Bash

∑ PowerShell Tutorial

PowerShell vs Bash
By Xah Lee. Date: 2009-07-26. Last updated: 2016-08-19.

This pages shows the equivalent of PowerShell for common unix commands
related to text processing.

Simple Commands
The following bash commands have PowerShell alias. (but the options may not be
the same)

cd
pwd
ls
pushd
popd

cp
rm
rmdir
mv

cat

echo
set

ps
kill

clear
man

More Complex Commands


Purpose Bash + GNU utils PowerShell
ni ff -type file
create new file touch ff
ni ↔ New-Item
cat ff
show file content cat ff
cat ↔ Get-Content ↔ gc
◇ cat f1 f2 > new.txt cat f1, f2 > new.txt
show first n lines head -n 50 ff cat ff | select -first 50
◇ tail cat file | select -last 50

◇ split ?

Purpose Bash + GNU utils PowerShell


list dirs find . -type d gci . -Recurse -name
gci ↔ Get-ChildItem
http://xahlee.info/powershell/PowerShell_for_unixer.html 1/5
3/21/2018 PowerShell vs Bash

◇ find . -type f ?
◇ find . -name "*html" gci . -Recurse -name -include *html
◇ find . -size 0 ls . -recurse | where {$_.length -eq 0}

◇ find . -type f -size 0 -exec rm {} \; ?

Purpose Bash + GNU utils PowerShell


search text grep xyz f.txt select-string f.txt -pattern xyz -CaseSensitive
search text grep xyz *html select-string *html -pattern xyz -CaseSensitive
search text grep -i select-string without -CaseSensitive
search text grep --invert-match select-string with -NotMatch
search text grep --files-with-matches ◇

cat file_name | where { $_ -match "regex_pattern"}

cat file_name | %{$_ -replace


"regex_pattern","replace_string"}

Purpose Bash + GNU utils PowerShell


diff
◇ cmp
diff ↔ compare ↔ Compare-Object
compare file difference diff f1 f2 diff (cat f1) (cat f2)
◇ sed ?
print nth column awk '{print $12 , $7}' ?
◇ sort sort (Sort-Object)

◇ uniq sort -Unique


Measure-Object
◇ wc
(measure ↔ Measure-Object)
◇ tr ◇
◇ basename ◇
◇ dirname ◇

Work In Progress
Note: this page is a mess. It's work in progress. (started when i wished to convert
my log processing bash script to PowerShell)

todo, shew PowerShell equivalent of this:

find . -print0 | xargs -0 -l -i echo "{}";


find . -name "*bmp" -print0 | xargs -0 -l -i basename -s ".bmp" "{}" | xargs -0 -l -i convert

Detail
touch

# creating a new file in current dir bash


touch myfile.txt

# creating a new file in current dir PowerShell


new-item -name myfile.txt -type "file"

Redirect “>”

http://xahlee.info/powershell/PowerShell_for_unixer.html 2/5
3/21/2018 PowerShell vs Bash

# put content in a file


echo "some" > myfile.txt bash
echo "some more" >> myfile.txt # append

# put content in a file PowerShell


"some" > myfile.txt
"some more" >> myfile.txt # append

Note that, by default, the PowerShell redirect operator ">" creates files with little
endian utf-16 encoding, and lines are hard-wrapped at 80 chars, and line ending
uses Windows convension of "\r\n" (ascii 13 and 10).

On unixes, the conventional file encoding is utf-8, and lines are not hard-wrapped
(sometimes truncated (deleted) silently), and line ending uses "\n" (ascii 10).

To create unix style output, use out-file, like this:

"1'n2'n3" | out-file -Encoding utf8 -width 999000 myfile.txt PowerShell

However, the line ending used is still "\r\n". To create unix line ending of just "\n",
use:

… | Out-String | %{ $_.Replace("`r`n","`n") } | out-file … PowerShell

However, the end of the file will still contain a "\r".

cat

Unix “cat” can be used to read a file, or join several files. PowerShell equivalent is
“get-content” with alias “cat” too.

# display a file content. (cat is alias of get-content) PowerShell


cat myfile.txt

Note that by default, PowerShell assumes ascii. You can set your
$OutputEncoding like this:

# set $OutputEncoding to utf-8 PowerShell


$OutputEncoding = New-Object -typename System.Text.UTF8Encoding

Thanks to Shivashis Saha for addition on “cat”. He also sends the following:

split

For example, if you want to split a line based on ":", you can use the following line:

(given $str is a line with different fields separated by ":") PowerShell


$temp=@($str -split ":");

Super thanks to Jeffrey Snover of Microsoft for helping on about 10 of the items.
(Jeffrey's the inventor of PowerShell)

Ask me question on patreon

http://xahlee.info/powershell/PowerShell_for_unixer.html 3/5
3/21/2018 PowerShell vs Bash

PowerShell
1. Install, Win, Mac, Linux
2. PowerShell Help
3. PowerShell as cmd.exe
4. list Alias, find Alias
5. Piping Output and Input
6. Environment Variables

1. Predefined Variables
2. Creating PowerShell Scripts

PowerShell vs Bash

http://xahlee.info/powershell/PowerShell_for_unixer.html 4/5
3/21/2018 PowerShell vs Bash

6 Comments xahlee.org 
1 Login

Sort by Best
 Recommend ⤤ Share

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Mohnish • 2 years ago


To get top N lines, we can use the TotalCount flag of Get-Content cmdlet. It should be better than reading the full file and
then picking few lines out of it, unless Powershell does some optimization automatically.

So, head -n 50 ff => cat ff -TotalCount 50


and cat ff | head 50 => cat ff | select -first 50
1△ ▽ • Reply • Share ›

Jonathan Alfonso • 3 years ago


Definitely useful, thank you for this. I am a Linux system admin, so I obviously have my fun shell scripts, and porting them to
Windows has been difficult (if not impossible!). Thanks a ton!
1△ ▽ • Reply • Share ›

Sean Crago • 3 years ago


One thing I'm missing: df/du would be useful, but is that built-in?
1△ ▽ • Reply • Share ›

Xah Lee Mod > Sean Crago • 3 years ago

am sure it is, or with a pipe. I haven't used PowerShell for years. Am back on linux now.
△ ▽ • Reply • Share ›

youHelpedAStranger • 4 years ago


Thanks so much! This is really useful. Btw I found this searching "powershell cat alternative" on duckduckgo.
1△ ▽ • Reply • Share ›

Avian • 4 years ago


sls is an alias for Select-Object. Also You can do sls xyz f.txt -ca as a shorthand for select-string f.txt -pattern xyz -
CaseSensitive - Select-String accepts both the pattern and the filename as positional arguments, and all cmdlet arguments
in Powershell can be reduced to the shortest prefix that uniquely identifies the argument name.
△ ▽ • Reply • Share ›

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Privacy

∑XAH
© 1995, …, 2018 Xah Lee.

http://xahlee.info/powershell/PowerShell_for_unixer.html 5/5

Vous aimerez peut-être aussi