Deprecation of cmdlets

Monday, 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

Thursday, 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

Custom objects default display in PowerShell 3.0

Friday, 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

Wednesday, 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

Monday, 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

Counting objects in PowerShell 3.0

Monday, March 19, 2012

Consider the following command, how many objects are in $dir? PS> $dir = Get-ChildItem The most common way to find it is to check the Count property: PS> $dir.Count But… the Count property is available only if Get-Something returns more than one object (array/collection), If the result has one object only (scalar) the Count property returns nothing, not even zero. If the result contains more than one object, the objects are accumulated in an Array (collection), and Arrays have a Count property so we get the number of objects. Now back to the question above, how...
one comment

Auto reconnect to a server you just rebooted with PowerShell 3.0

Sunday, January 8, 2012

Yesterday I saw a tweet that caught my eye: Sounds familiar? What do you usually do to reconnect to your server once it is back online? How do you monitor it’s availability? In this post, Justin shares a great function (115 lines) that restarts a server, waits for it to come back online and then starts a remote desktop connection to it. Now, what if I told you that in PowerShell 3.0 you can replace that function with only two lines of code? In PowerShell 3.0, the Restart-Computer cmdlet got improved (mainly to support Workflow scenarios) and it now supports...

PSRR – Remote Registry PowerShell 3.0 Module

Wednesday, November 2, 2011

var addthis_config = {"data_track_clickback":true}; One of the new improvements in the .NET Framework version 4 is the Microsoft.Win32.RegistryView enumeration. On the 64-bit version of Windows, portions of the registry are stored separately for 32-bit and 64-bit applications. There is a 32-bit view for 32-bit applications and a 64-bit view for 64-bit applications. Many of the 32-bit keys have the same names as their 64-bit counterparts, and vice versa. In the 64-bit version of Registry Editor, 32-bit keys are displayed under the following registry key: HKEY_LOCAL_MACHINE\Software\WOW6432Node. I’m pleased to announce that the PSRemoteRegistry module has...

New advanced functions language features in PowerShell v3

Monday, October 24, 2011

var addthis_config = {"data_track_clickback":true}; PowerShell 3.0 CTP1 includes many new language enhancements. Here are four of them for advanced functions.   CmdletBinding and Parameter attributes We are no longer required to assign a value of $true to CmdletBinding and Parameter attributes. For example, in v2 we had to write:   param( [Parameter(Position=0, ...
no comments

Windows PowerShell 3.0 CTP1 is available for download!

Tuesday, September 20, 2011

var addthis_config = {"data_track_clickback":true}; Windows Management Framework 3.0 Community Technology Preview (CTP1) is available for download for Windows 7 SP1 and Windows Server 2008 R2 SP1. Read the full announcement on the PowerShell team blog. Check out this post on the PowerShell Magazine site for a quick review of new cmdlets and parameters in v3. var addthis_config = {"data_track_clickback":true};
one comment