Reputation: 641
I have a Powershell Script, Which Contains:
$Path = 'c:\windows\system32\notepad.exe'
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($Path)
$Icon.ToBitmap().Save('C:\icon.png')
I write those script in the 'PwerShell ISE' when I run, it work's fine, and not problem show's up.
But when saving those script, and run by right click on 'icon.ps1' then 'Run With PowerShell' it will get this error:
PS D:\My PowerShell\tmp> .\icon.ps1
Unable to find type [System.Drawing.Icon].
At D:\My PowerShell\tmp\icon.ps1:2 char:9
+ $Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($Path)
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Drawing.Icon:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
You cannot call a method on a null-valued expression.
At D:\My PowerShell\tmp\icon.ps1:4 char:1
+ $Icon.ToBitmap().Save('C:\icon.png')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Upvotes: 1
Views: 1376
Reputation: 140
You need to load the System.Drawing assembly before you run the rest of the script. At the beginning of the script, put this line:
Add-Type -AssemblyName System.Drawing
This will load the assembly first, allowing the rest of the script to access the methods and members of that assembly.
Upvotes: 2