You’ll be pleased to know I have spent many hours tidying it up, making links to other cmdlets internal to the document throughout and it has also been thoroughly reviewed by fellow PowerShell MVPs Thomas Lee , Richard Siddaway and Aleksandar Nikolic – I am most appreciative of their efforts in reviewing and the feedback given!

I hope you find this consolidated series useful. If you do then I ask that you consider making a small donation to a UK based charity that help the parents of children born with Tracheo-Oesophageal Fistula (TOF) and Oesophageal Atresia (OA). You can find out more about this charity through their website http://www.tofs.org.uk/index.php and can make a donation here http://www.justgiving.com/tofs/donate . I know from personal experience what a great job they do. Members of this charitable organisation condition are being particularly encouraged during October 2010 to raise awareness and funding so I hope you will help me in doing that.

Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Add-Type cmdlet.

What can I do with it?

Imbed code from modern programming languages into your PowerShell session or scripts. The list of valid languages are: C#, C# 3.0, VisualBasic and JScript – C# is the default. Use the Language parameter to specify one if it is not C#.

Example:

Within a PowerShell session use some C# code to create a TakeAway class and create a static method Minus. Use the Add-Type cmdlet to add the class to the session and then call the TakeAway class and Minus static method.

Enable text in scripts displayed to users to be presented in their own language. The cmdlet uses the automatic variable $PSUICulture to determine the language to use and alternate text is stored within .psd1 files in subdirectories of the folder that the script is stored.

Example:

In a script called RegionalTest.ps1 use the ConvertFrom-StringData cmdlet to create a series of text messages to display to the user. Import-LocalizedData will retrieve the value of the $PSUICulture automatic variable, get the contents of the RegionalTest.psd1 file in the es-ES directory (assume the user is Spanish) and store the data within the variable designated by the BindingVariable parameter. Then display the Welcome text.

Configure strict mode for the current scope. An error will be generated when the content of an expression, script or script block violates coding rules. Note: it is possible to use the Version parameter to pick which coding rules to use. The PowerShell help lists the current possible options as:

Add, Remove or Replace items from a property value of an object. This cmdlet can only update a property when it supports the IList interface. So far this does not include any of the core Windows PowerShell cmdlets – however it does include some of the cmdlets that ship with Exchange 2007 and later.

Example:

Add additional email addresses to the Test1 user’s mailbox using the Add parameter of Update-List.

Notice the PSSessionConfiguration on a machine enabled for PowerShell remoting

Disable PowerShell Remoting. You will receive the following warning that using this command does not necessarily reverse everythign that Enable-PSRemoting may have done.

WARNING: Disabling the session configurations does not undo all the changes made by the Enable-PSRemoting or
Enable-PSSessionConfiguration cmdlet. You might have to manually undo the changes by following these steps.
1. Stop and disable the WinRM service.
2. Delete the listener that accepts requests on any IP address.
3. Disable the firewall exceptions for WS-Management communications.
4. Restore the value of the LocalAccountTokenFilterPolicy to 0, which restricts remote access to members of the
Administrators group on the computer.

Now check the impact on the PSSessionconfiguration – the AccessDenied permission has been applied to Everyone.

How could I have done this in PowerShell 1.0?

Remoting did not exist in PowerShell 1.0, you would have needed to use Remote Desktop to run an interactive session on a remote server.