PowerShell Tid Bits #1


The more I use power shell the more I realize you can do just about anything you could do with visual studio and C#. I haven’t found a comprehensive source for all the little differences between C# and powershell but the following are some things to take note of:

First, PowerShell seems to have access to the entire .NET framework however it’s import to remember that you may need to adjust PowerShell to run at the version level you require. You just locate where your powershell.exe and powershellise.exe are located and edit the powershell.exe.config and powershellise.exe.config files respectfully. ( source: https://stackoverflow.com/questions/2094694/how-can-i-run-powershell-with-the-net-4-runtime )

Example:

<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>

 

You can even add parts of the .NET framework that aren’t loaded automatically with (source: https://tfl09.blogspot.com/2010/08/using-newer-versions-of-net-with.html )

Add-Type -Path "Path to .dll file"

 

If you wish to call aspects of the .net frame work it’s extremely easy to do for instance say you wanted to do string.IsNullOrEmpty(stringToTest) well in PowerShell you would do the following:

$stringToTest = ""

Write-Host [string]::IsNullOrEmpty($stringToTest)

I haven’t yet found a way to do the C# string $”Value1={$class.Value1}” in PowerShell in such a neat and simple way. When you do that same thing in PowerShell you just return the class object rather then the value you wanted. So in order to achieve that same result you would need to do

Write-Host "Value1=" + $class.Value