I see this question often: how can I pass a parameter value for a PSCredential that might be a credential object or it might be a user name? In the past I’ve used code like this: begin { Write-Verbose -Message “Starting $($myinvocation.mycommand)” write-verbose -Message “Using volume $($volume.toUpper())” #convert credential to a PSCredential if a string…
Category: PowerShell
Friday Fun: Get Next Available Drive Letter
A few days ago I saw on question, I think on Facebook, about using PowerShell to find the next available drive letter that could be used for mapping a network drive. Before I show you my approach, let me state that if you need to map a drive in PowerShell for use only within your…
Try and Catch Me If You Can
In looking at entries in this year’s Scripting Games, as well as posts I see in PowerShell forums, I thought I’d post a short guide to properly using Try/Catch. This is the way I think it should be used. Let’s start with a Try/Catch block that might look ok. Try { Get-Service Foo } Catch…
Pipeline Power
Last week I came across a blog post that had a decent example using PowerShell and PowerCLI to get the disk location for all virtual machines. The posted code works and does display the information you might be after. $myVMs = get-vm foreach($vm in $myVMs){ $myDisks = @($vm | get-harddisk) foreach ($disk in $myDisks) {…
Create an HTML PowerShell Help Page
Yesterday I posted an article about getting the online url for a cmdlet help topic. Today I want to demonstrate how we might take advantage of this piece of information. Since the link is already in the form of a URL, wouldn’t it make sense to put this in an HTML document? At first glance,…
Get Cmdlet Help URL
I was toying around with PowerShell help this morning and as usually happens one thing leads to another. When you run Get-Help, or use the wrapper function Help, you are actually getting an object: MamlCommandHelpInfo. This object has properties that you are use to seeing like name and synopsis. PS C:\> get-help get-service | Select…
Have Your Output and Variable Too
There’s a relatively useful suggestion floating around on Twitter on how to save results of PowerShell command to a variable and see the results at the same time. PS C:\> ($data=get-process) I’ll admit this is a clever technique: you get the results from Get-Process written to the pipeline AND a variable $data. The other way,…
Valentines Day PowerShell Fun
In case you missed some of the fun on Twitter and Google Plus, here are the PowerShell valentines I sent today. These are intended to be run from the PowerShell console, not the ISE. Also, depending on your console font, you might get slightly different results. I use Lucida Console. #1 write-host (([char]3…
Friday Fun PowerShell Valentines Day
With Valentine’s Day just around the corner, I thought I should help you out with your gift giving. Remember those bags of candy with the cute sayings like “Be Mine”? Here’s how you can create a “bag” of them using Windows PowerShell; perfect for that extra geeky significant other. Or maybe you’ll just have fun…
Ping a Service with PowerShell
The other day I came across a PowerShell question on StackOverflow about testing if a service was running on a group of machines.This sparked an idea for a tool to “ping” a service, in much the same way we ping a computer to see if it is up and running, or at least reachable. It…
Training Down Under
I will be doing a private #PowerShell training class in Canberra, Australia in March. Since it is a long trek I’d love to add a second week of work, either training or speaking. I had a second training gig lined up but it fell through. I realize it might be short notice but I’d love…
Export and Import Hash Tables
I use hash tables quite a bit and with the impending arrival of PowerShell 3.0 I expect even more so. PowerShell v3 allows you to define a hash table of default parameter values. I’m not going to to cover that feature specifically, but it made me realize I needed a better way to export a…
Friday Fun: A PowerShell Alarm Clock
Today’s Friday Fun is a continuation of my exploration of ways to use Start-Job. A few weeks ago I wrote about using Start-Job to create “scheduled” tasks. I realized I could take this further and turn this into a sort of alarm clock. The goal is to execute at command at a given time, but…
Convert Text to Object Updated
I’ve had a few comments and emails lately about my post and script on converting text to objects. I decided the function needed a little more lovin’ so today I have an updated version, complete with comment based help. Function Convert-TextToObject { tasklist /s server01 /fo list | where {$_} | convert-texttoobject -group 5 Take…
The PowerShell Morning Report
I love how easy it is to manage computers with Windows PowerShell. It is a great reporting tool, but often I find people getting locked into one approach. I’m a big believer in flexibility and re-use and using objects in the pipeline wherever I can. So I put together a PowerShell script that I can…