The other day I was answering a question in the PowerShell Facebook group. This person was getting data from Active Directory and trying to organize the results in a way that met his business requirements. My suggestion was to use Group-Object and a custom grouping property. I am assuming you are familiar with Group-Object. You…
Tag: ScriptBlock
Skipping WMI System Properties in PowerShell
One of my favorite techniques when using WMI in PowerShell is to pipe an object to Select-Object and select all properties. Try this: get-wmiobject win32_bios | select * It works, but it also gets all of the system properties like __PATH which I rarely care about. I also get other properties like Site and Options…
Friday Fun: 13 More Scriptblocks
In celebration of Friday the 13th I thought I would offer up a menu of 13 more script blocks. If you missed the first course, you can find the original 13 scrptblocks here. I’m not going to spend a lot of time going over these. Many of them are simple one liners. Some of them…
PowerShell Scripting with [ValidateScript]
The last few days we’ve been looking at parameter validation attributes you might use in a script of function. Yesterday I wrote about [ValidateRange] and demonstrated how you might use it. That attribute works fine for any values that can be evaluated as numbers. But dates are a different story. I got a comment with…
Scripting with PSCredential
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…
Re-Use PowerShell Scriptblocks
//Re-Use PowerShell Scriptblocks// I commented on a blog post today that showed how to use a hash table with Select-Object to format file sizes say as KB. dir $env:temp -rec | select fullname,@{Name="KB";Expression={$_.length/1kb}} Since you most likely will want to use something similar for other directories, don't feel you have to re-invent the wheel. Save…
Friday Fun What a CHAR!
Last week I posted a PowerShell snippet on Twitter. My original post piped an array of integers as [CHAR] type using an OFS. Don’t worry about that. As many people reminded me, it is much easier to use the -Join operator. -join [char[]](116,103,105,102) I’ll let you try that on your own. The [CHAR] type is…
The PowerShell Day Care: Building ScriptBlocks
Good morning kids and welcome to the PowerShell Day Care center. We offer a creative and nurturing environment for PowerShell professionals of all ages. Later there might even be juice and cookies. But first let’s get out our blocks, our scriptblocks, and start building. I’ve written a number of posts on script blocks and today…
Get Properties with Values
One of my nuisance issues when using WMI with Windows PowerShell, is that when looking at all properties I have to wade though many that have no value. I’d prefer to only view properties that have a populated value. Here’s one way.
ScriptBlocks On the Fly
I’m always preaching about writing PowerShell scripts and functions with reuse and modularization in mind. You should never have to write the same block of code twice. But what about in the shell during your daily grind? Perhaps today you’re dealing with some issue and periodically need to run a particular block of code. Now,…
Friday Fun: Start-TypedDemo v2
Not too long ago I posted a function I wrote for doing PowerShell demonstrations. My goal was to simulate a live interactive demo but without the typing so I could focus on explaining and not typing. The first version was a good start but I always had plans for a more feature complete function including…
Friday the 13 Script Blocks
In celebration of Friday the 13th and to help ward off any triskaidekaphobia I thought I’d offer up 13 PowerShell scriptblocks. These are scriptblocks that might solve a legitimate business need like finding how long a server has been running to the more mercurial such as how many hours before I can go home. A…
Out-Clip
I’ve started working on the 2nd edition of Managing Active Directory with Windows PowerShell: TFM. As with almost all of my writing projects it will be full of PowerShell code examples. In the past I’ve always relied on a manual copy and paste to add content to the manuscript. The PowerShell Community Extensions made this…
Potential Pipeline Pitfall
Last week I was helping someone out in the PowerShell forum at ScriptingAnswers.com. The specific problem is irrelevant;l however I learned something in the process that will affect how I write my own PowerShell functions from now on. Not being a developer I never picked up on this subtle (at least to me) distinction. Here’s…
The PowerShell Balloon Festival
I trust by now you are realizing how valuable Windows PowerShell is as a management tool. With a one line command you can accomplish an extraordinary amount of work. Sometimes this work may be long running, which is where background jobs come in handy. Or you may simply kick off a long running script and…