1
votes

In my powershell script I open a FolderBrowserDialog to get a path from User. I'd like to allow User to create new folder but I get this message regardless of folder selected.

[Window Title] Invalid location

[Content] You can't create a new folder here. Choose a different location.

Example error dialog window

I thought maybe I need to elevate the execution policy on PowerShell, but that didn't make a difference.

# [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
Add-Type -AssemblyName System.Windows.Forms

$FBD = New-Object System.Windows.Forms.FolderBrowserDialog
$FBD.ShowNewFolderButton = $True
$FBD.RootFolder = "MyComputer"
$FBD.SelectedPath = "C:\"
$FBD.Description = "Navigate and select directory"
$Choice = $FBD.ShowDialog()
if ($Choice -eq "OK")
{
    Write-Host "Selected Path: " $FBD.SelectedPath
}

UPDATE Additional info:

  • I can manually create folder inside explorer
  • I can create folder within powershell with 'New-Item', without elevated privilege

Things I've tried without success:

  • Running terminal with elevated privileges
  • Using Python TKinter filedialog (same issue)?!

All in all it seems like a Windows permission issue with calling Windows Forms.

1
Does the user running the form actually have access to create new folders in C: (or wherever you navigate to)? - Mathias R. Jessen
@MathiasR.Jessen: The user is able to create folder manually and inside powershell with 'New-Item'. Do forms have their own permissions? - ynwa
No, that's not the issue then. Does this happen everywhere in the folder hierarchy, or in a specific place? - Mathias R. Jessen
It doesn't seem to be limited to a specific location. I tried multiple hierarchies with same result. - ynwa

1 Answers

0
votes

You don't have admin rights to the location or you're trying to access a restricted area that you need to run the script in an elevated PowerShell terminal. No need to call c# when you can do it much simpler.

 New-Item -Path "c:\" -Name "NameOfFolder" -ItemType Directory

enter image description here