Saturday, August 14, 2010

Batch to Powershell: Title

If you try to set the title of the window in powershell, 'title' won't work; but you can set the title using the $host object PS C:\> $Host.UI.RawUI.WindowTitle = "Donkey" You can also mess with other fun things like colors eg: PS C:\> $Host.UI.RawUI.BackgroundColor = [ConsoleColor]::Red If you're a big title user and have title burned into your muscle memory you can always create function called title e.g.: PS C:\Temp> function title ($t) {$host.UI.RawUI.WindowTitle=$t }PS C:\Temp> title Donkey2 Have fun moving into powershell....

Wednesday, August 4, 2010

Powershell: Returning ScriptBlocks and Closures

Powershell lets you return scriptblocks (aka anonymous functions) from functions, for example: function generateAdder ($n){ { param ($x) $n+$x }} PS C:\> $add4 = generateAdder 4PS C:\> & $add4 77If you've used languages with closures you'd have expected to get back a function that adds 4 to a passed in value. You'd expect that because $n=4 was in lexical scope when we created the scriptblock to be returned. Recall powershell is dynamically scoped so we don't need to bind ($n) to a lexcial location, instead we can pick up $n from our current environment: PS C:\>...

Sunday, August 1, 2010

Batch to Powershell: dir /s

I often go looking for a file in batch, aka c:\Program Files (x86)>dir /s fsi* Volume in drive C has no label. Volume Serial Number is 8EDE-D64E Directory of c:\Program Files (x86)\Microsoft F#\v4.0 03/19/2010 02:02 PM 230,216 Fsi.exe 09/30/2009 08:08 PM 158 Fsi.exe.config 2 File(s) 230,374 bytesUnfortunately this doesn't 'just work' in powershell. A quick search on the internet shows in powershell 'dir /s' becomes 'dir -r', but the following doesn't work either: PS C:\Program Files (x86)> dir -r fsi*PS C:\Program Files...

Page 1 of 2312345Next