Using .NET Types Directly

by Feb 23, 2016

Cmdlets contain pure .NET code, so thanks to cmdlets, you do not need to directly touch .NET code. You can, however. Here are a number of sample calls that illustrate how .NET methods can be accessed:

#requires -Version 2
[System.Convert]::ToString(687687687, 2)

[Math]::Round(4.6)

[Guid]::NewGuid()

[System.IO.Path]::ChangeExtension('c:\test.txt', 'bak')

[System.Net.DNS]::GetHostByName('dell1')
[System.Net.DNS]::GetHostByAddress('192.168.1.124')

[Environment]::SetEnvironmentVariable()

# dangerous, save your work first
[Environment]::FailFast('Oops')

Add-Type -AssemblyName PresentationFramework
$dialog = New-Object Microsoft.Win32.OpenFileDialog
$dialog.ShowDialog()
$dialog.FileName

Twitter This Tip! ReTweet this Tip!