PowerShell Team
Automating the world one-liner at a time…
Latest posts
Resolve-ShortcutFile
If you're like me, you've got a browser favorites folder that is full of wonderful PowerShell code snippets. Ideally, it would be great to have a way to bring those favorite snippets into PowerShell so that my favorites folder becomes a code library. Doing this presents several challenges. The first (and easiest to solve) is getting the web page the shortcut points to out of the .url file. .url files are in the same sort of format as .ini files (each line has a property name, and =, and the value). So to get the url out of a shortcut file, I simply had to Get-Content on the file, pipe it to Where-Object, look f...
Get-MarkupTag
On my personal blog (Media And Microcode), I've been posting a series called "Scripting the Web", which introduced a function called Get-MarkupTag. Get-MarkupTag is a very handy little function that coerces individual tag elements of a web page into HTML, so you can scrape data from a webpage. I've updated Get-MarkupTag a tiny bit for CTP3, marking the tag name as value from pipeline so I can get multiple tag types from a single document. I'm posting it again here so that a wider audience can make use of it, and so that I can use it in some later blog posts. Since I've already got inline help for the f...
Advanced Functions and Test-LeapYear.ps1
Welcome to CTP3! There are just an amazing amount of really important features in V2 but the one I want everyone to start using ASAP is Advanced Functions. In CTP2 we called this cmdlets. We dropped that term in favor of taking the idea of a FUNCTION and just making it more and more capable. It is a bit of a misnomer in that you can have an "Advanced function" by just adding a single line to your script. You'll be amazed what that single line gives you; but we'll explore that later. This entry recaps where we are with functions and then begins to give you a feel for some of the...
More Advanced Functions – Get-Assembly and Get-ExportedType
Tyson Kopczynski has just published a great article Fun with PowerShell 2.0 Eventing! That inspired to to write a script to automatically backup my modules directory. I'm doing a lot of scripting this week and I don't want to lose my work if I make a mistake. I thought I would write a scripting using PowerShell 2.0 Eventing to watch my directory and then make a backup copy automatically. The way to do this is with a FileSystem Watcher or something like that. Here is where the story changes and why the title of this blog is not "Backup Your Modules Automatically with PS V2 Eventing". ...
PowerShell V2: ParameterSets
One of the great benefits of PowerShell V2 Advanced Functions is the ease in which you can support parametersets. ParameterSets are, well, different SETS of valid parameters. For instance you can say: Get-Process -id 0 Get-Process -Name *ss Those are 2 different parametersets for the Get-Process cmdlet. Here is an example of how you would code parametersets using advanced functions: function test-param { param( [Parameter(ParameterSetName="p1",Position=0)] [DateTime] $d, [Parameter(ParameterSetName="p2", Position=0)] [int] $i ) switch ($PsCmdlet.ParameterSetName) &n...
Windows PowerShell CTP2 to CTP3 Conversion Guide
I write a lot of scripts, and, since I blog some of what I write, my home computer has been running Windows PowerShell CTP2 since it came out. Since CTP3 has a number of changes from CTP2, I've got to update my home script library to work with CTP3. While this guide might not have every change that happened in between CTP2 and CTP3 (because I script with most, but not all, of PowerShell), I hope that this record of conversion steps helps everyone else moving from Windows PowerShell CTP2 to CTP3. Oisin Grehan has posted a fairly complete list of cmdlet name changes on his blog, Nivot Ink. T...
Dreaming In PowerShell V2 : Lottery Numbers with Get-Random
Tobias Weltner writes a blog called Dreaming In PowerShell, and he recently posted a way to get a list of unique lottery numbers with PowerShell. Dreaming In PowerShell is a cool blog, and the post is interesting, but it makes an assertion that's no longer true in V2. He uses System.Random to create the random numbers because he asserts that there isn't a cmdlet to Get random numbers. Get-Random is a V2 cmdlet that can not only give you random numbers, but can give you unique random numbers within a range My CTP3 version is on the left, Tobias' V1 version is on the right: The core function...
Mount-SpecialFolders.ps1
The other day I showed you how you can use ENUMs. I showed an example of how you can find all the SPECIALFOLDERs on a system. Here is a script that I call Mount-SpecialFolders.ps1 which mounts all your special folders as PowerShell drives. # Mount-SpecialFolders.ps1 # param($Folder="*", [SWITCH]$Verbose, [SWITCH]$PassThru) foreach ($f in [Enum]::GetValues([System.Environment+SpecialFolder]) |where {$_ -like $Folder}) { $drive = New-PSDrive -Name $f -PSProvider FileSystem -Root ([Environment]::GetFolderPath($f)) -Scope Global -ErrorAction Silen...
What the Heck is an ENUM?
In my latest post Explore your [Environment] I said the following: PS> [System.Environment+SpecialFolder] IsPublic IsSerial Name BaseType -------- -------- ---- -------- Fa...