Saturday, 10. April 2010 21:31
Ok, we all know modules in Powershell 2.0 are very cool… For me, probably the best of all newly introduced concepts is namespace support.
To make long story short – if I import function Get-Command from module SCCM, I can access it using namespace – SCCM\Get-Command.
Looks really pretty, is secured and you can use consistent function names in different modules, which is perfect from usability perspective.
You don’t need to use namespaces if you don’t want to however. So you can either use SCCM\Get-Command OR Get-Command.
Problem is that when you use Import-Module, last one always wins. So my SCCM\Get-Command still works fine, however of course it will overwrite Get-Command.
I think this is extremely dangerous behavior. If you also thing it should be changed, please vote here. My proposal is that Import-Module should get switch that will identify that all exported objects are accessible by specifying namespace also.
As a workaround, you can use following code:
Get-Command -CommandType cmdlet | ForEach-Object {Set-Alias -Name $_.Name -Value "$($_.PSSnapIn)\$($_.Name)"}
Insert this code at beginning of your script and it will take care that you won’t be able to accidentally overwrite existing cmdlets (aliases always wins).
Martin