PowerShell 5.1.14393

On August 2nd, 2016 Microsoft released the Windows 10 Anniversary Update. It will be pushed out via Windows Updates as the “Cumulative Update for Windows 10 Version 1607“. This update will update your PowerShell version from 5.0.10586.494 to the latest version.

PS C:\Development> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  0

PS C:\Development>

PowerShell contains what are called “Automatic Variables“. These are variables that are automatically defined in every PowerShell script. The $args variable contains an array of the undeclared parameters and/or parameter values that are passed to a function, script, or script block. When you create a function, you can declare the parameters by using the param keyword or by adding a comma-separated list of parameters in parentheses after the function name.

Until this recent update, you could override any of these variables to make it do whatever you want. For example if I were to execute the following code in the previous version of PowerShell it would execute just fine.

[System.Collections.ArrayList] $args = New-Object -TypeName 'System.Collections.ArrayList'
 

However, after the update you can no long override an Automatic Variable by changing its type. You will get the following error if you attempt to do so:

PS C:\Development> [System.Collections.ArrayList] $args = New-Object -TypeName 'System.Collections.ArrayList'
At line:1 char:1
+ [System.Collections.ArrayList] $args = New-Object -TypeName 'System.C ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cannot assign automatic variable 'args' with type 'System.Object[]'
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AssignmentStatementToAutomaticNotSupported

PS C:\Development>

To fix the problem you simply change your variable name to an unused variable name and it will work correctly without throwing an error. For a complete list of variables that are in scope, you can run the following PowerShell command:

PS C:\Development> Get-Variable | Out-String

Name                           Value
----                           -----
$                              clear
?                              True
^                              clear
args                           {}
ConfirmPreference              High
ConsoleFileName
DebugPreference                SilentlyContinue
Error                          {}
ErrorActionPreference          Continue
ErrorView                      NormalView
ExecutionContext               System.Management.Automation.EngineIntrinsics
false                          False
FormatEnumerationLimit         4
HOME                           C:\Users\ericDorothy
Host                           System.Management.Automation.Internal.Host.InternalHost
InformationPreference          SilentlyContinue
input                          System.Collections.ArrayList+ArrayListEnumeratorSimple
MaximumAliasCount              4096
MaximumDriveCount              4096
MaximumErrorCount              256
MaximumFunctionCount           4096
MaximumHistoryCount            4096
MaximumVariableCount           4096
MyInvocation                   System.Management.Automation.InvocationInfo
NestedPromptLevel              0
null
OutputEncoding                 System.Text.ASCIIEncoding
PID                            15664
PROFILE                        C:\Users\ericDorothy\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
ProgressPreference             Continue
PSBoundParameters              {}
PSCommandPath
PSCulture                      en-US
PSDefaultParameterValues       {}
PSEdition                      Desktop
PSEmailServer
PSHOME                         C:\Windows\System32\WindowsPowerShell\v1.0
PSScriptRoot
PSSessionApplicationName       wsman
PSSessionConfigurationName     http://schemas.microsoft.com/powershell/Microsoft.PowerShell
PSSessionOption                System.Management.Automation.Remoting.PSSessionOption
PSUICulture                    en-US
PSVersionTable                 {PSVersion, PSEdition, PSCompatibleVersions, BuildVersion...}
PWD                            C:\Development
ShellId                        Microsoft.PowerShell
StackTrace
true                           True
VerbosePreference              SilentlyContinue
WarningPreference              Continue
WhatIfPreference               False



PS C:\Development>

Now I know you’ve all been very good PowerShell developers and have not tried to override the automatic variables for your own gain. If not, then I hope that this little update informed you. Thanks, and I’ll see you next time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s