Techibee.com

PowerShell: Show/Get a browse folder window

In today’s post, let us see some more useful methods inside shell.application com object. In previous post, I showed you how to get desktop icons status. In this post, I will demonstrate, how to display a window using which user can browse and select folder. You know what I am talking? Look at the below screenshot for clarity.

You no need to use any C#, dotnet, WPF stuff to get this. The script completely replies on Shell.Application COM object. This COM object has a method called BrowseForFolder using which we can generate this window. The selection is returned in the form of a object which we can process to get the path user selected via the window.

Code:

function Get-OutputFolder {            
 [CmdletBinding()]            
 Param(            
 )            

 $Shell = New-Object -ComObject "Shell.Application"            
 $Folder = $Shell.BrowseForFolder(0,"Choose Output Folder",0)            
 if($Folder) {            
  return $Folder.self.Path            
 } else {            
  Write-Warning "No folder selected"            
 }            

}

Output:

Happy learning.

Exit mobile version