DCSIMG
PowerShellV3 - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

News


btn_donate_LG

View Shay Levy's profile on LinkedIn Follow Shay Levy at Twitter Shay Levy's Facebook profile Subscribe to my FriendFeed


site statistics




Browse by Tags

All Tags » PowerShellV3 (RSS)
Deprecation of cmdlets
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...
Creating objects with a cast in PowerShell 3.0
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: [System.Drawing.Point]@{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...
Custom objects default display in PowerShell 3.0
In PowerShell 3.0 we can now create new custom objects using a hash table. PS> [PSCustomObject]@{ 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 PSCustomObject over New-Object is property order. PSCustomObject preserve the...
Add-Member enhancements in PowerShell 3.0
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...
Measuring objects in PowerShell 3.0
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...
Counting objects in PowerShell 3.0
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...
Auto reconnect to a server you just rebooted with PowerShell 3.0
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...
PSRR - Remote Registry PowerShell 3.0 Module
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...
New advanced functions language features in PowerShell v3
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: [ CmdletBinding ( SupportsShouldProcess = $true ) ] param ( [ Parameter ( Position = 0 , ValueFromPipeline = $true , ValueFromPipelineByPropertyName = $true ) ]...
Windows PowerShell 3.0 CTP1 is available for download!
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};
How to disable positional parameter binding in PowerShell vNext
var addthis_config = {"data_track_clickback":true}; Windows PowerShell supports two kind of parameters: Named and Positional. In a nutshell, Named parameters must be specified on the command line while Positional parameters are inferred by the argument’s position on the command line. The type of the parameter is controlled by the [Parameter()] attribute (see about_Parameters ). What if we wanted to disable positional parameters and force users to write the parameter name and its value?...