Windows
Windows

Making Powershell more Bash-like


I realize powershell is its own thing and I’m not trying to turn it into Bash.

But I work on windows, mac and linux machines throughout my day and often end up typing in bash commands into powershell to get the dreaded error message:

printenv : The term 'printenv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:1
+ printenv
+ ~~
    + CategoryInfo          : ObjectNotFound: (printenv:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Then have to translate it into something that works for powershell. To be fair, I am really happy that the powershell team does support a number of commands out of box like ls -> dir but find lots of other commands missing with difficult to remember conversions.

So, much like bash, you have a nice Alias command that will let you override words to other meanings. I quickly realized its more complicated, the alias command doesn’t like parameters. You can work around it by creating functions that will call those parameters. So to get started, lets edit the $PROFILE (much like .bashrc or .bash_profile)

Commands

  • printenv
  • ll
  • touch
  • nano

notepad $PROFILE
(you may need to create the file first, it is in ~\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 )

New-Alias -Name "ll" -Value "ls" # not really equivalent, but at least it doesn't error out
function printenvOnPowershell {
    gci env:
}
New-Alias -Name "printenv" -Value "printenvOnPowershell"
New-Alias -Name "nano" -Value "micro" #install micro at https://github.com/zyedidia/micro

function touchOnPowershell {
  $file = $args[0]
  if($file -eq $null) {
    throw "No filename supplied"
  }
  if(Test-Path $file){
    (Get-ChildItem $file).LastWriteTime = Get-Date
  }else{
    echo $null > $file
  }
}
New-Alias -Name "touch" -Value "touchOnPowershell"

echo "Microsoft.Powershell_profile.ps1 done"

Ongoing Updates

I plan to keep adding to this page as I find more desired bash commands.

openanalytics 220282 views

I'm a 35 year old UIUC Computer Engineer building mobile apps, websites and hardware integrations with an interest in 3D printing, biotechnology and Arduinos.


View Comments
There are currently no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.