Posts

Showing posts from October, 2008

PowerShell: which for windows

As Stephen Mills points out in a comment below, all this is unnecessary because there's a cmdlet that does this even better. Get-Command So please disregard this post :P Fresh code out of the oven. I was on my Windows XP box and needed to know where I had an specific executable file stored. Some Windows boxes have a similar command called where but it wasn't availed in my box. So I just coded one in PowerShell. function which ($file){ $return = @() if (Test-Path ".\$file" -ea silentlycontinue){ # check current dir $return +=@(".\$file") } foreach ($path in (get-content env:path).Split(";")){ # check all paths in PATH env var if (Test-Path "$path\$file" -ea silentlycontinue){ $return += @("$path\$file") } } return $return } Off course it can be refined - and probably should - but it's good enough for most situations. Ex: > which notepad.exe C:\WINDOWS\system32\notepad.exe C:\WINDOWS\notepad.exe PS: I store...

PowerShell: very simple FTP client

One of my first experiments with PowerShell, besides ls and HelloWorld.ps1, was trying the object oriented features and the .Net Framework. I was at the time thinking about a problem I was having with an FTP client I was using and I asked myself: "Would it be possible to make an FTP client with PowerShell?" I knew there was a .Net class that implemented an FTP client so I googled and bit and found it . After reading the examples shown and a little try-and-error, I came up with this extremely simple FTP client in PowerShell below. Note that by "extremely simple" I mean "with very few features and no error detection code". This only gets one file from the FTP server. I probably wouldn't use this in production without a few changes. But it's still a nice example on how to use the .Net framework with PowerShell and this class in particular $localfile = "c:\file.txt" $remotefile = "/folder/file.txt" $ftphost = "ftp://ftpserver...

PowerShell

A few weeks ago I had a chance to attend a workshop about PowerShell entitled "Using PowerShell" by Ed Wilson . Fantastic teacher and fantastic person to talk to about anything. This was my first time using PowerShell. I had tried one of Monad's first versions but it took so much time to load and crashed so many times I decided to wait for a final version. I ended waiting a bit more than the final version though. My first impression was "This is fantastic!! ". And it really is. Being a lover of the command line and an avid user of Bash and other UNIX shells, I welcome PowerShell. I'm not going to compare Bash and PowerShell and try to guess the best shell. In my humble opinion, they're not comparable. For that kind of entertainment, google for PowerShell vs Bash. You'll probably get a lot of results with links to endless and pointless discussions between UNIX and Windows zealots. PowerShell and Bash have very few similarities. Quoting Ed Wilson ...