The Scripting Games are approaching and we don't know yet whether they will be asking us to prefer the use of Powershell V3 over V2, nor whether V3 scripts will get better points. If in doubt, I think that we must be ready to use the latest version possible.
One possible problem that you could face if you are using PowerGUI to write your scripts, is that it defaults to Powershell V2, as you can see issuing:
One possible problem that you could face if you are using PowerGUI to write your scripts, is that it defaults to Powershell V2, as you can see issuing:
$PSVersionTable.psversion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
The tip to start PowerGUI linked to Powershell V3 is to start the ScriptEditor with the parameter "-version 3.0" by modifying its shortcut.
The same command will then return:
Major Minor Build Revision
----- ----- ----- --------
3 0 -1 -1
Now you might ask what is the interest of using V3. Well, the answer is easy: there are new cmdlets, and the existing cmdlets have new parameters.
Let's take the examples of the last Scripting Guy post.
The where-object cmdlets (whose alias is a question mark '?') used a Filterscript to remove object from the pipeline:
Get-Process | ? {$_.starttime} | select name, starttime
In Powershell V3 a new Property parameter has been wisely added to speed up things:Get-Process | ? starttime | select name, starttime
Not only this is faster to write, but it is also much faster in terms of performance. Let's ask measure-command to check this for us:
- cls
- $counter = 9
- $counter2 = 9
- $h = 0
- $i = 0
- "Pass`tProperty`tFilterscript`tProperty`tFilterscript"
- 1..$counter | %{
- $h ++
- Write-Progress -id 1 -Activity "Checking where-object duration" -PercentComplete ($h / $counter * 100)
- 1..$counter2 | %{
- $i ++
- $var1 = $var1 + [int](measure-command -Expression {get-Process | ? starttime | select name, starttime} | select totalmilliseconds).totalmilliseconds
- $var2 = $var2 + [int](measure-command -Expression {Get-Process | ? { $_.starttime} | select name, starttime} | select totalmilliseconds).totalmilliseconds
- $var3 = $var3 + [int](measure-command -Expression {get-Process | ? starttime | select name, starttime} | select totalmilliseconds).totalmilliseconds
- $var4 = $var4 + [int](measure-command -Expression {Get-Process | ? { $_.starttime} | select name, starttime} | select totalmilliseconds).totalmilliseconds
- Write-Progress -id 2 -Activity "Cycling through" -PercentComplete ($i / $counter2 * 100)
- }
- $sumvar1 = [int]($var1/$counter2)
- $sumvar2 = [int]($var2/$counter2)
- $sumvar3 = [int]($var3/$counter2)
- $sumvar4 = [int]($var4/$counter2)
- $totalvar1 += $sumvar1
- $totalvar2 += $sumvar2
- $totalvar3 += $sumvar3
- $totalvar4 += $sumvar4
- $array1 = @("Pass $h";$sumvar1;$sumvar2;$sumvar3;$sumvar4)
- $array1 -join "`t "
- $var1 = $null
- $var2 = $null
- $var3 = $null
- $var4 = $null
- $i = 0
- }
What I do here is to run two tests each for "? starttime" and "? { $_.starttime}" and to wrap them into 9 cycles of 9 (just to get a representative average) and put the results into an array whose content is displayed in form of a table on screen by using concatenation (-join).
Anyway what matters here is not the script but the result, which is self-explanatory (duration is in milliseconds):
Pass Property Filterscript Property Filterscript Pass 1 10 13 9 19 Pass 2 8 11 8 12
Pass 3 13 17 13 17
Pass 4 10 11 9 16
Pass 5 9 13 9 12
Pass 6 9 11 8 12
Pass 7 13 17 15 16
Pass 8 11 12 11 13
Pass 9 13 13 11 13
The average duration of where-object drops dramatically when using the new Property parameter instead of the old Filterscript, and this is a good reason to me to move to Powershell V3.
No comments:
Post a Comment