Powershell Tricks

Here are two things that I haven't found written about other places, but that are hinted at in the help for New-Object.

Open an explorer window in the current directory:

> $shell = New-Object -comObject shell.application
> $shell.Explore("$(resolve-path .)")  

 

You can present a folder selection dialog to a user:

> $shell.BrowseForFolder(0, "Title", 0, "c:\") | get-member


   TypeName: System.__ComObject#{a7ae5f64-c4d7-4d7f-9307-4d24ee54b841}

Name                       MemberType Definition
----                       ---------- ----------
CopyHere                   Method     void CopyHere (Variant, Variant)
DismissedWebViewBarricade  Method     void DismissedWebViewBarricade ()
GetDetailsOf               Method     string GetDetailsOf (Variant, int)
Items                      Method     FolderItems Items ()
MoveHere                   Method     void MoveHere (Variant, Variant)
NewFolder                  Method     void NewFolder (string, Variant)
ParseName                  Method     FolderItem ParseName (string)
Synchronize                Method     void Synchronize ()
Application                Property   IDispatch Application () {get}
HaveToShowWebViewBarricade Property   bool HaveToShowWebViewBarricade () {get}
OfflineStatus              Property   int OfflineStatus () {get}
Parent                     Property   IDispatch Parent () {get}
ParentFolder               Property   Folder ParentFolder () {get}
Self                       Property   FolderItem Self () {get}
ShowWebViewBarricade       Property   bool ShowWebViewBarricade () {get} {set}
Title                      Property   string Title () {get}

 

Use above to select something:

> $shell.BrowseForFolder(0, "Title", 0, "c:\").Self.Path

 

This MSDN page gives the information you need for the third argument.  It is a 'flags' field that can be any combo of ulFlags on that page.

See $shell | Get-Member for more.

Comments

# re: Powershell Tricks

Saturday, October 04, 2008 5:28 AM by Leaf

The folder browser is a nice idea, thanks, but had to mention that you can get an explorer folder in the current directory (or any other) using the invoke-item command. Using the preconfigured invoke-item alias gives you this command line to open explorer:

> ii .

# re: Powershell Tricks

Tuesday, October 07, 2008 8:33 AM by jeffesp

That's great to know.  Thanks!

Leave a Comment

(required) 
(required) 
(optional)
(required)