blog.griff.systems

Search

Search IconIcon to open search

Bash Command Alternatives in PowerShell

Last updated Oct 23, 2023

Bash and PowerShell are two popular command-line shells that can help you automate tasks and manage your system. Bash is the default shell for Linux and macOS, while PowerShell is the default shell for Windows. If you are an IT professional who has some experience in using Bash, but need to administer a Windows environment, this might just be helpful to you. In this post, we’ll take a look at some common Bash commands and their equivalent (more or less) commands in PowerShell.

General Commands

Below is a quick cheat sheet of common Bash commands and how to accomplish the same thing in PowerShell. Many of the commands are pre-aliased in PowerShell and will work as you’d expect, while others require use of different commands to accomplish the desired outcome.

BashBash Command works in PoShPowerShell CmdletPowerShell AliasPurposeComment
catNoGet-ContentgcRead content from a file.
clearYesClear-HostclearClear the terminal.
cpYesCopy-ItemcopyCopy an item.
curlYesInvoke-WebRequestiwrRequest data from a URL.To truly match the default wget behavior, you’ll need to wrap the command in parenthesis then use the .Content dot operator, or pipe the command’s output into Select-Object Content.
diffYesCompare-ObjectdiffCompare content of two objects.
grepNoSelect-StringslsFind a matching string.To truly match the default grep behavior, you’ll need the -Pattern and -SimpleMatch parameters.
historyYesGet-HistoryhShow previous commands executed at the terminal.
killYesStop-ProcesskillEnd a process.
lsYesGet-ChildItemgciList items in a directory.
manYesGet-HelpmanView command help file.
mkdirYesmkdirmdCreate a directory.
mvYesMove-ItemmiMove an item.
psYesGet-ProcesspsView a running process.
pwdYesGet-LocationglShow the present working directory.
rmYesRemove-ItemrmRemove an item.
tailNoGet-ContentgcRead the contents of a file.To truly match the default tail behavior, you’ll need the -Wait parameter.
teeYesTee-ObjectteeRedirect output to two locations.
wgetYesInvoke-WebRequestiwrRequest data from a URL.To truly match the default wget behavior, you’ll need to wrap the command in parenthesis then use the .Content dot operator, or pipe the command’s output into Select-Object Content.

It’s important to note that while both the PowerShell Cmdlet or Alias can be used functionally, when writing scripts, you should always use the full Cmdlet name instead of the alias.

Editing Files

Systems Administrators and other IT professionals who have worked with Linux before will know the convenience of being able to edit a file directly in the terminal using text editors such as vim or nano or others. These two editors in particular are nice because they come pre-packaged with many Linux distributions.

Unfortunately, Windows does not have such functionality in PowerShell. Luckily, we can install both Vim and Nano on Windows. While Windows does have a newly inbuilt package manager, winget, I still prefer chocolatey for its ease of use, reliability, and wide selection of available software.

If you don’t have Chocolatey and want to use it to install these editors, you can install using this command as of the time of this post’s writing:

1
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

Note: You can check out the [Chocolatey website]( Chocolatey Software | Installing Chocolatey) to verify if this is still the current install script.

With that installed (and after restarting your terminal), you can install both these editors:

1
2
3
4
5
# Install Nano text editor
choco install -y nano

# Install Vim text editor
choco install -y vim

You’ll want to restart your terminal again since this will modify your profile, but after that you’re all set to edit a file like you usually would in Bash:

1
nano C:\Path\To\Your\File

I prefer Vim, and even once the package is installed, you’ll need to use vim as your editor command unless you want to additionally create an alias for it:

1
New-Alias -Name "vi" -Value "vim"

Now you can use Vim to edit files in the terminal to your heart’s content:

1
vi c:\Path\To\Your\File

Conclusion

In this post we looked at some common Bash commands and their equivalents in PowerShell, as well as how to unlock the power of in-terminal text editing in PowerShell just like we have in Bash. While administering a Windows environment can be quite different from a Linux environment, there are many niceties that you can still take advantage of to make life easier. I definitely encourage any IT professional to learn more about PowerShell and its capabilities! You can find more resources on PowerShell on the Microsoft Learn website. Keep those servers humming!