Improving the output of Update-Help

December 20, 2012

Source: PowerShellMagazine.com   If you’ve updated PowerShell to version 3.0 you probably noticed that help is no longer shipped in the box. PowerShell 3.0 includes a new feature, the Updatable Help system, which allows you to ensure that help on the local computer stays up to date. PowerShell is now a part of the operating system and operating systems gets to be updated only during service packs or specific patches. The Updatable Help system and its cmdlets (*-Help) make it easy to download and install help files, or updating exiting help files, as soon as newer...

Skipping empty CSV objects

December 4, 2012

Hi, it’s been a while since my last post, I’ve been busy mostly at work and also by investing most of my time running the PowerShell Magazine website together with my friends. I hope to post more in the future. For the time being, here’s my latest post, cross-posted on the PowerShell Magazine. Working with CSV files in PowerShell is a common practice. You import the file, loop on its records and you’re good to go. Sometimes however you may find yourself in a situation where you get a file that has blank lines in it, and those...
12 comments

Deprecation of cmdlets

April 30, 2012

Until PowerShell 3.0, if you shipped a cmdlet there was no good way to deprecate it. Cmdlet developers can use the .NET ObsoleteAttribute now to let users know that they should not use a cmdlet anymore. In the following example, the cmdlet can run but it will issue a warning that another cmdlet should be used from now on. This gives the user time to prepare for the change and fix any scripts dependent on old commands. $code = @' using System; using System.Management.Automation; namespace ObsoleteCmdlet { ...
208 comments

Creating objects with a cast in PowerShell 3.0

April 19, 2012

Starting with PowerShell 3.0, we now have another cool way to create new objects. We can create them by calling the default constructor of the type and initializing its properties with a cast: @{X=1;Y=2} We put the type in square brackets and assign it a hash table containing the properties we want to initialize. Looking at this example got me thinking, how can I know the properties I can set for a specific class/type? As a learning tool, I wrote the Get-HashType function. Give it a type name and it will tell you the properties...
18 comments

How to protect your PowerShell Functions

April 17, 2012

Creating functions in PowerShell is relatively a simple operation. You declare the function’s name and value, press the Enter key and you’re good to go. It is also very simple to override the function just by re-creating it; that’s all it takes, simple and easy. What if you need to prevent that from happening? You don’t want to allow someone or something from overriding your functions? PowerShell gives us the option to do that via a dynamic parameter. Dynamic parameters are cmdlet parameters that are added by a Windows PowerShell provider and are available only when...
one comment

Custom objects default display in PowerShell 3.0

April 13, 2012

In PowerShell 3.0 we can now create new custom objects using a hash table. PS> @{ One = 1 Two = 2 Three = 3 Four = 4 Five = 5 } One : 1 Two : 2 Three : 3 Four : 4 Five : 5 Behind the scenes, PowerShell creates a hash table and wraps it a PSCustomObject. It is way faster than using the New-Object cmdlet and it also provides consistency, while maintaining backwards compatibility. Another benefit of using...

Add-Member enhancements in PowerShell 3.0

April 11, 2012

PowerShell 3.0 offers new ways to add Note properties to objects. In PowerShell 2.0, a typical command to do that would look like: PS> New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name One -Value 1 -PassThru One --- 1   And when adding multiple note properties: PS> New-Object -TypeName PSObject | Add-Member -MemberType NoteProperty -Name One -Value 1 -PassThru | Add-Member -MemberType NoteProperty -Name Two -Value 2 -PassThru | Add-Member -MemberType NoteProperty -Name Three -Value 3 -PassThru One Two Three --- --- ----- 1 ...
24 comments

Measuring objects in PowerShell 3.0

April 9, 2012

The Measure-Object cmdlet gives us a great way to find minimum and maximum values in a collection of objects. For example, if we want to know the smallest and largest size of a file in the current directory: PS> Get-ChildItem | Measure-Object -Property Length -Minimum -Maximum Count : 2751 Average : Sum : Maximum : 56297240 Minimum : 35 Property : Length In PowerShell 2.0 it only worked with numeric properties (integers), we couldn't use it to compare properties like LastWriteTime (DateTime). In PowerShell 3.0 we can now use...
one comment