Powershell.

by gorthx

Yes, here we are again, with me using a Windows machine. I can’t decide if Powershell makes having to use Windows tolerable, or just throws salt in the wounds. Powershell provides much more efficient methods of searching files and moving/renaming them than messing with Exploder, but every time I need it, I have to look up the syntax because it’s just not familiar.

Here are samples of the commands I use regularly, so they’re all in one place & I can easily C&P them from anywhere.

Find all .zip files:
Get-ChildItem -path c:\path\to\search -recurse -filter *zip

Order of the options is not important, and recurse can be shortened to rec.

Find a certain file somewhere on my hard drive:
Get-ChildItem -path c:\ -filter settings.xml -rec

I search file content a lot, so I made an alias for grep (also in my profile), because it’s easier for me to remember:
Set-Alias grep select-string

Find my notes about JSON, somewhere on my hard drive:
Get-ChildItem -path c:\ -inc *.txt -rec | grep -pattern "json"
…this is a case-insensitive search.

Convoluted way to move files (still looking for something easier):
Get-ChildItem -path c:\old\path -rec -filter *zip | foreach-object { copy-item -path $_.fulllname -destination c:\new\path }

If your paths or filenames include spaces, you’ll have to quote them, of course.

There is a way to diff files but I find the output nearly unusable.

Additional tips:
– You don’t have to type the commands in in camel case; powershell will transform it.
– There is some tab-completion available.
– I added this to my profile to save my history between sessions: https://lopsa.org/content/persistent-history-powershell. There’s no up/down arrow paging for commands from a previous session, though; you have to list the history items and then execute them from the menu. (With a command e.g. “i 2”. Yeah, that’s intuitive. Feels like the 80s in here.)

And: <esc> for <ctrl>-u.


Useful links:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb613488(v=vs.85).aspx
http://www.powershellatoms.com/desktop-management/creating-persistent-aliases-in-powershell/
http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/27/use-powershell-to-copy-files-to-a-shared-drive.aspx

Advertisements
Tags:

2 Comments to “Powershell.”

  1. Compare-Object (diff) with Powershell is pretty intuitive to me. You have to read out the text inside each file which is done by using Get-Content and putting them both in parens. Then Compare-Object compares both the arrays. I guess I’m just used to the Powershell way of doing this.

%d bloggers like this: