Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

There is nothing that can’t be done with PowerShell

2010-10-04

Basically, what makes PowerShell a so much better command line shell and scripting language than its competitors, is that you can do anything and everything. If you want to create scripts, you must give yourself permission to do so (shown here). In addition, if you are using Windows 7 or later, you must start PowerShell as an administrator to be able to give yourself permission to run scripts.

The secrets behind the versatility of PowerShell is the power of the syntax, excellent type management, and access to both COM components and the .NET Framework. My twitter example is really just an example of accessing the .NET Framework from PowerShell. I could just as well have shown how to convert a JPG image to PNG, which by the way could be done like this:

#Load the Drawing library (no Enter strokes here):
[System.Reflection.Assembly]::Load("System.Drawing, Version=2.0.0.0, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

#Load a JPG.
$img=New-Object System.Drawing.Bitmap("C:\Anders\mypicture.jpg")

#Save the desiered format in a variable.
$format=[System.Drawing.Imaging.ImageFormat]::Png

#Save as PNG.
$img.Save("C:\Anders\converted.png", $format)

#Delete the object.
$img.Dispose()

So, let’s say that you want to access a COM object from PowerShell. You would still use the New-Object, but you append the -ComObject argument. This gives me a handle to Excel (if you have it installed):

$xl=New-Object -ComObject Excel.Application

Now, you have state of the art spread sheet automation at your fingertips. You can display it like so:

$xl.Visible=$true

How about that!?

Categories: PowerShell

Tags: PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips