Same Command. Different Return Types.

My command seems to behave differently depending on how many items were returned. This is something that the PowerShell Team hears from the community or our internal partners every couple of weeks. This blog post will explain when a Windows PowerShell command returns different types and offer some reasons for why it is done this… Read more

Installing PowerShell on Vista/"Longhorn" Server

1.       I am new to PowerShell and would like to install it on my Vista machine. The following page  http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx gives you links to various PowerShell downloads. Choose the one that best matches your operating system, platform and language requirements. 2.       I am using a “Longhorn” build greater than 5600 and I do not find… Read more

Why Can’t I Pipe Format-Table to Export-Csv (and get something useful)?

Ken’s PowerShell blog has an entry Remote Services and PowerShell where he wrote a function to get services from a remote machine using WMI. He formatted the data using FT (format-table) and then went on to export the data to CSV. He pointed out that the following would NOT work rget-service remotemachinename netlogon | ft… Read more

Why isn’t “New-Object” aliased to “New”

Alistair Young made a Note to Self : In a standard PowerShell installation, “new” is not an alias for “new-object”. You added that. You should therefore know better that to make this kind of dumbass mistake. We talked about making “New” an alias to “New-Object” but decided against it in the end because it leaves… Read more

PowerShell Cheat Sheet Redux – the PDF version

Attached is a PDF version of Ben Pearce’s PowerShell Cheat Sheet.  In the past I’ve encouraged everyone to speak up and complain if we were messing up or not giving you what you needed.  I’m pleased to see that you have taken this to heart (seriously).   Ben and every other Microsoft employee has been using Office 12 for… Read more

PowerShell Cheat Sheet

Ben Pearce was gracious enough to put together a good looking PowerShell Cheat Sheet which I’ve included as an attachment to this blog post.  By definition, a cheat sheet is incomplete so this contains the things that Ben has found to be the most import.   I think you’ll find it useful. Cheers! Jeffrey Snover [MSFT]Windows PowerShell/MMC ArchitectVisit… Read more

Windows PowerShell Exit Codes

Windows PowerShell Exit Codes   PSMDTAG:FAQ: How can my script control the PowerShell exit code? Answers: 1.      A normal termination will set the exitcode to 0 2.      An uncaught THROW will set the exitcode to 1 3.      The EXIT statement will stop the process and set the exitcode to whatever is specified.   c:\>powershell -noprofile… Read more

The Wonders of Date Math using Windows PowerShell

Larry Hayden posted a query about dates at:http://techtasks.com/bookmarks/powershell/2006/09/interview-with-a-scripter-jeffrey-snover/ He has a script which gets all the Application Events that happened TODAY: ————————————————————$today = [DateTime]::Today$Events = Get-Eventlog -New 1024 Application | Where {$Today -le $_.TimeWritten}$Events |Group EntryType,Source |Format-Table Count,Name -Auto———————————————————— What Larry really wants is the Application Events that happend in the last 24 hours.  We’ll… Read more

ErrorLevel equivalent

PSMDTAG:FAQ: ErrorLevel – what is the PowerShell equivalent? In Cmd.exe, %ErrorLevel% is a builtin variable which indicates the success or failure of the last executable run. In PowerShell, we support:     $?       Contains True if last operation succeeded and False otherwise.And     $LASTEXITCODE       Contains the exit code of the last Win32 executable execution. PS> 2+24PS> $?TruePS>… Read more

DateTime Utility Functions

I often want to find things that happened Today.  For instance, which files got changed today.  Windows PowerShell makes this easy to do but it can be a bit verbose and I do it a lot so I’ve added a function to my profile:  IsToday. function isToday ([datetime]$date) {[datetime]::Now.Date  -eq  $date.Date} This takes advantage of… Read more