COM pt3–WScript.Shell

One thing that we seem to forget when we use PowerShell is the functionality that was available to us in VBScript through the Windows Scripting Host (WSH) objects. Much of this functionality has been duplicated in PowerShell cmdlets or through .NET classes but there is still a good pile of stuff we can use.  I think its time to stir up that pile and have a look at what we can find. The great thing about this functionality is:

  • its available on all Windows boxes
  • there are many VBScript examples we can reuse

The WScript object is the top of the tree as far as WSH is concerned bit it doesn’t really do much for us. Lets drop down a level and look at the WScript.Shell object.

 

PS> $wshell = New-Object -ComObject “WScript.Shell”
PS> $wshell

 

Name                     MemberType
—-                     ———-
AppActivate              Method
CreateShortcut           Method
Exec                     Method
ExpandEnvironmentStrings Method
LogEvent                 Method
Popup                    Method
RegDelete                Method
RegRead                  Method
RegWrite                 Method
Run                      Method
SendKeys                 Method
Environment              ParameterizedProperty
CurrentDirectory         Property
SpecialFolders           Property

 

The properties look interesting lets start there.  Special folders are the desktop etc which we will look at later. Current directory is obvious.

The Environment property is listed as a parameterized property – this means we have to give it a value

001
002
003
004
005
006
$wshell = New-Object -ComObject “WScript.Shell”
“System”
, “User”, “Process”, “Volatile” |
foreach {
“`n$($_)”
$wshell.Environment($($_))
}

 

We have four possibilities which gives us a nice break down of the environmental variables.  It supplies a bit more info than

dir env:

The Environment property may look like a method but it isn’t really.

We do have a set of methods to play with

AppActivate
CreateShortcut
Exec
ExpandEnvironmentStrings
LogEvent
Popup
RegDelete
RegRead
RegWrite
Run
SendKeys

which we will start looking at later.

This entry was posted in COM, PowerShell V2, WSH. Bookmark the permalink.

1 Response to COM pt3–WScript.Shell

  1. cavallogolooso says:

    🙂

Leave a comment