Friday, October 8, 2010

Taskmgr to PowerShell: Kill A Process

You know the drill, some app freezes. You try to close it, that doesn’t work.  You fire up taskmgr, sort by name, type in the name, and then hit the delete key to kill the process.  Easy with powershell:

Get-Process | ? {$_.Name -eq "firefox"} | kill


The real power here is the composability of powershell, for example you could also debug the process.



Get-Process | ? {$_.Name -eq "firefox"} | Debug-Process


Or if you’re not sure what’s going to happen you can always pass the –whatif qualifier:



PS C:\Users\igord\Downloads> Get-Process | ? {$_.Name -eq "firefox"} | Debug-Process -whatif
What if: Performing operation "Debug-Process" on Target "firefox (8204)"


Happy Powershelling

Wednesday, October 6, 2010

Photo Fusing in Windows Live Photo Gallery

On my camera, I can make the self timer take 8 photos 2 seconds apart. This leaves me with a 1 in 8  chance of getting a good picture. However, using the photo fuse tool in Windows Live Photogallery, I can do a visual merge of the photos to pick my favorite part of each photo.  For example, I used the following source photos:

IMG_1349IMG_1352IMG_1353IMG_1354

IMG_1355IMG_1356IMG_1357

To created this composite photo, (To which I also applied straiten and auto fix)

StaceyDaveAndFamFuse

That’s seriously cool!

Saturday, September 18, 2010

PowerShell: Tab Complete History

If you haven’t tried PowerTab or haven’t tried PowerTab recently stop, and check it out .  PowerTab makes powershell a real pleasure to use, and might be the feature that gets me over the powershell dip.

Once you’ve installed PowerTab , you can history complete by starting your command with a #, typing some letters and then hitting tab. For example:

powertabHistoryCompletion

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 4
PS C:\> & $add4 7
7

If 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:\> $n = "Hello"
PS C:\> & $add4 7
Hello7

What if you want to instantiate a closure with powershell instead? Luckily someone thought of that and you can do:

function generateAdder2 ($n)
{
{
param ($x)
$n+$x
}.GetNewClosure()
}


PS C:\> $add4 = generateAdder2 4
PS C:\> & $add4 7
11


Leave a comment if you use these features, I’m curious what you’d use them for.

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 bytes

Unfortunately 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 (x86)>

What's going on? Let's check the help:

PS C:\Program Files (x86)> help dir

NAME
Get-ChildItem

SYNOPSIS
Gets the items and child items in one or more specified locations.


SYNTAX
Get-ChildItem [[-Path] ] [[-Filter] ] [-Exclude ] [-For
ce] [-Include ] [-Name] [-Recurse] [-UseTransaction] []

Get-ChildItem [-LiteralPath] [[-Filter] ] [-Exclude ]
[-Force] [-Include ] [-Name] [-Recurse] [-UseTransaction] []


Ahh, path and filter are two separate arguments in powershell. This means we get 2 choices.



A) Pass a path to the dir command (pass ‘.’ for current directory):



PS C:\Program Files (x86)> dir . fsi* -r

Directory: C:\Program Files (x86)\Microsoft F#\v4.0


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/19/2010 2:02 PM 230216 Fsi.exe
-a--- 9/30/2009 8:08 PM 158 Fsi.exe.config
PS C:\Program Files (x86)>

B) Explicitly pass the -filter argument name (which we can abbreviate to -fi)

PS C:\Program Files (x86)> dir -r -fi fsi*


Directory: C:\Program Files (x86)\Microsoft F#\v4.0


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/19/2010 2:02 PM 230216 Fsi.exe
-a--- 9/30/2009 8:08 PM 158 Fsi.exe.config
PS C:\Program Files (x86)>


OK you’re thinking who cares, I can still do dir /s faster in batch? Ah, but you can’t do this:



 PS C:\Program Files (x86)> dir -r -fi fsi* | Where-Object {$_.Length -gt 200KB}


Directory: C:\Program Files (x86)\Microsoft F#\v4.0


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/19/2010 2:02 PM 230216 Fsi.exe
PS C:\Program Files (x86)>


Have fun, and remember read the docs when things don’t work as you’d expecxt:)

Saturday, July 17, 2010

F# Tricks: Concise BoolToInt

As a C# programmer, you'd probably write BoolToInt as follows in F#:
	let boolToInt3 b = 
if b = true then
1
elif b = false
0


Not a big win over C# yet, but a few things to point out about the concise syntax: Indentation implies blocks. There is no 'return' keyword. This function is statically typed, you don't see types since the compiler infers the types.



As a novice F# programmer a trick you learn is pattern matching. Pattern matching is like a case statement, but it's an expression and it's really powerful. I'll go into more details on pattern matching in future posts. For now, notice the elegance of the below:



	let boolToInt2 b = 
match b with
| true -> 1
| false -> 0

Pattern matching is so useful to F# there is a syntax for creating a function of one parameter that only pattern matches, using that syntax, we define boolToInt in the final form of:

	let boolToInt3 = function 
| true -> 1
| false -> 0