Marius Gheorghe

Building software : make it work, make it good, make it fast

Few tips and tricks for using Powershell as a shell language (part I)

Navigating around the filesystem

The first thing you have to do is add some "shortcuts" to your usual paths. The simplest way to do that is to edit the PS profile file (you can find this file in [[MyDocuments]]\WindowsPowerShell\Microsoft.PowerShell_profile). The "shortcuts" can be added in 2 ways (with advantages and disadvantages) :

- add the shortcut as a variable which points to the right path.

$dx = 'c:\dropbox'

To use it just cd %dx and you'll navigate to c:\dropbox. The advantage of using vars is that you can reuse them if you write some code interactively. The disadvantage is that you'll always have to "cd" manually to actually navigate to that path.

- if you don't really care about "reusing" that variable, you should do declare it like as a function:

function %dx
   cd c:\dropbox

To use it just type %dx and you'll navigate to that path. Also i really recommend you to use a standard naming convention for this (i prefix the "shortcuts" with the % char).
For navigation around the filesystem, "ls" is used to list the files/subfolders in the current path. Obviously you can also filter the result like this :

ls *.pdf

This will list just the pdf files from current path.

File operations

With PS , you can use the old DOS commands (like mkdir, del etc), the PS "native" commands or even the Linux utilities ( try GOW the lightweight Linux alternative ). Personally i prefer the old DOS commands, but the important thing to note here is you have the freedom to use 3rd party utilities instead of the builtin cmdlets if they do a better job. You can even add functions to your PS profile that invoke other utilities with built in cli parameters to save you some typing.

So here's how i do most filesystem operations:

- delete
The DOS command is del myfile.txt to delete a single file. It also works with wildcard selector del my*.jpg code>
The PS "native" way is using the Remove-Item cmdlet. Something like Remove-Item myfile.txt. But, obviously, you don't want to type "Remove-Item" each time so you can use the alias "rm".
If you prefer the "unix" way, just run the GOW rm utility: rm myfile.txt

- create folders / files
mkdir myFolder to use DOS/Linux command or New-Item MyFolder to use the builtin cmdlet. For files you usually want to edit it after creation. Personally i just run vim to create and edit the file using a function defined in my profile. Something like vimedit a.txt where vimedit will just run vim with the specified path as a parameter

- copy/move files
Copying and moving files from the command line is not the most straightforward thing ever but it's manageable. Usually i use copy/xcopy (built in Windows tools) or robocopy . Personally, i never really remember all the cli switches to do more advanced copy/move operations so i usually rely on FarManager (since it can be runned inside the terminal)

- grepping for text inside files For finding specific text pattern inside multiple files i'm using a Select-String wrapper like :

function psgrep($path, $fileType, $string)
   get-childitem $path -include $fileType -rec | select-string $string

Use it like
psgrep c:\oss\ *.cs "static" . This will search for the keyword "static" in filetypes *.cs from path c:\oss

- searching for files by name :
I use a Get-ChildItem wrapper like this
function psgrepFile($path, $fileType)
  get-childitem $path -include $fileType -rec
Invoke it like this :
psgrepFile c:\oss a*.xml