How to use FolderBrowserDialog correctly in Powershell

So, I'm still pretty new to Powershell and I'm trying to write a script that allows the user to select a file or folder and then return the security permissions for the specified folder / file. The problem is I cannot find the file path to write as a variable to be used later. Here's what I have so far:

Function Get-Folder($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.rootfolder = "MyComputer"
    $foldername.ShowDialog()

    if($foldername.ShowDialog() -eq "OK") {
        $folder += $foldername.SelectedPath
    }
}

      

I might have gotten away with this, but it pulls up a window to select a file or folder and forces me to select twice and then not set the variable as the file path. Again, I'm pretty new to this sort of thing, so I might be completely wrong, but any help would be incredibly helpful.

Thank!

+6


source to share


3 answers


The folder selection window appears twice because you have two calls $foldername.ShowDialog()

. Delete the first one and leave only one inside if

.

I tried to run your code and I'm pretty sure the variable is $folder

actually set. If you think it is not installed, you are doing something wrong. For example, keep in mind that it is only visible inside your function Get-Folder

. If you need to use it outside of a function, you must return it ( return $folder

) and assign it to a variable outside of the function. For example:



Function Get-Folder($initialDirectory)

{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.Description = "Select a folder"
    $foldername.rootfolder = "MyComputer"

    if($foldername.ShowDialog() -eq "OK")
    {
        $folder += $foldername.SelectedPath
    }
    return $folder
}

$a = Get-Folder

      

This way you will have the selected folder in a variable $a

.

+13


source


You need to add "| Out-Null" to the end of the line "[System.Reflection.Assembly] :: LoadWithPartialName (" System.Windows.Forms ")"

Otherwise, there is a bunch of information returned by Get-Folder that you don't want



Cheers, Garth

+4


source


Folder selection

Use System.Windows.Forms.FolderBrowserDialog

only allows you to select a folder.

Function Get-Folder($initialDirectory) {
    [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
    $FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $FolderBrowserDialog.RootFolder = 'MyComputer'
    if ($initialDirectory) { $FolderBrowserDialog.SelectedPath = $initialDirectory }
    [void] $FolderBrowserDialog.ShowDialog()
    return $FolderBrowserDialog.SelectedPath
}
($FolderPermissions = Get-Folder C:\Users | get-acl | select -exp access | ft)

      

Read more about the TG42 class in the official docs .

File selection

function Get-File($initialDirectory) {   
    [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    if ($initialDirectory) { $OpenFileDialog.initialDirectory = $initialDirectory }
    $OpenFileDialog.filter = 'All files (*.*)|*.*'
    [void] $OpenFileDialog.ShowDialog()
    return $OpenFileDialog.FileName
}
($FilePermissions = Get-File C:\ | get-acl | select -exp access | ft)

      

Read more about the class System.Windows.Forms.OpenFileDialog

in the official docs .

+2


source







All Articles