# FolderBrowserDialog startingat last selected folder

**URL:** <https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435>\
**Category:** PowerShell Help\
**Created:** [November 19, 2019, 8:50am UTC](https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435 "2019-11-19T08:50:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bnwgraaf](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@bnwgraaf](https://forums.powershell.org/u/bnwgraaf)\
**Post date:** [November 19, 2019, 8:50am UTC](https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435/1 "2019-11-19T08:50:38Z")

</div>

I am using the FolderBrowserDialog in a Powershellscript.

Is it possible to remember the selected folder and use it next time as starting location for browsing folders? Now it starts everytime in the same folder (regardless if I use RootFolder and/or SelectedPath)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 19, 2019, 9:54am UTC](https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435/2 "2019-11-19T09:54:21Z")

</div>

It is hard to give an intelligent and helpul advice if you do not show some of the code you’re using. But anyway you can save any location in a variable and use it later on if you need.

---

<div class="post-metadata">

**Author:** ![bnwgraaf](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@bnwgraaf](https://forums.powershell.org/u/bnwgraaf)\
**Post date:** [November 19, 2019, 10:11am UTC](https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435/3 "2019-11-19T10:11:04Z")

</div>

Hello and tnx for your answer!

I will give some more information.

The code is part of an addin (called DataStandard) for Autodesk Inventor (3D CAD software) and Vault (Datamanagement software). With this addin a dialog pops up when saving a file for the first time. See [this Example](https://www.google.com/url?sa=i&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwiUyMuXzfblAhUDElAKHV4hDSsQjRx6BAgBEAQ&url=https%3A%2F%2Fforums.autodesk.com%2Ft5%2Fvault-customization%2Fvault-data-standard-2019-template-file-mass-property-pull-to%2Ftd-p%2F8372020&psig=AOvVaw3Z02tAr7DwfNk8_VTnzc1Q&ust=1574264120784111).

This DataStandard exists of several XAML files for the dialogs and ps1 files for the logic.

In the XMLfile I have made a button for selecting a folder for saving the file. This button points to a simple ps1 file for showing the FolderDialog. This ps1 file has this code:

[pre]function SelectFolder  
{

### Zie ook: [#PSTip: Using the System.Windows.Forms.FolderBrowserDialog Class](https://www.powershellmagazine.com/2013/06/28/pstip-using-the-system-windows-forms-folderbrowserdialog-class/)

$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog  
$FolderBrowser.RootFolder = “MyComputer”  
$FolderBrowser.SelectedPath = "C:\VaultWerkmap\Lightronics"  
$FolderBrowser.ShowNewFolderButton = “true”  
$FolderBrowser.ShowDialog()

$Prop[‘Folder’].Value = $FolderBrowser.SelectedPath.Replace(“C:\VaultWerkmap\Lightronics",”")  
}[/pre]

Buteverytime I use this DataStandard dialog, the folder dialog starts at the same folder. I have tried with and without the Rootfolder and SelectedPath lines.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 19, 2019, 10:23am UTC](https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435/4 "2019-11-19T10:23:27Z")

</div>

I’ve used a similar function in the past and it worked just as expected …

```
function Select-Folder {
    [CmdletBinding()]
    [OutputType([string])]
    param(
        [System.String]
        $PreSelection = 'C:\temp'
    )
    Add-Type -AssemblyName System.Windows.Forms
    $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
        SelectedPath = $PreSelection
    }
 
    [void]$FolderBrowser.ShowDialog()
    $FolderBrowser.SelectedPath 
}
```

When you call it without any path it will start in c:\temp but if you provide an existing path it will just start with this path preselected.

---

<div class="post-metadata">

**Author:** ![bnwgraaf](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@bnwgraaf](https://forums.powershell.org/u/bnwgraaf)\
**Post date:** [November 20, 2019, 2:09am UTC](https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435/5 "2019-11-20T02:09:58Z")

</div>

Hello Olaf,

Even if it is more or less the same code, I tried your code in my case, but it still returns to the preselected location everytime the folderbrowser dialog starts. Maybe this is happening, because everytime I am openeing the folderbrowser, the addin/schript is started again, so it starts as if it is started for the first time?

I mean, the script is not a constantly running programm, but is used by the addin in the CAD software (and does not “live” inside this CAD programm), and it will be started as a new instance of the script? I don’t know how I can explain it clearly…

Maybe I have to do it in some kind of dirty way by storing the last used path in a txt file.

---

<div class="post-metadata">

**Author:** ![bnwgraaf](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@bnwgraaf](https://forums.powershell.org/u/bnwgraaf)\
**Post date:** [November 20, 2019, 2:36am UTC](https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435/6 "2019-11-20T02:36:40Z")

</div>

Maybe it is not the most clean way, but it seems to work for me:

&nbsp;

[pre]function SelectFolder  
{

### Zie ook: [#PSTip: Using the System.Windows.Forms.FolderBrowserDialog Class](https://www.powershellmagazine.com/2013/06/28/pstip-using-the-system-windows-forms-folderbrowserdialog-class/)

# txt bestand om laatst geselecteerde folder in op te slaan

$savedLastSelectedFolder = “C:\ProgramData\Autodesk\Vault 2020\Extensions\DataStandard\CAD.Custom\addins\Selectedfolder.txt”  
$defaultFolder = “C:\VaultWerkmap\Lightronics”

# Als deze niet bestaat, dan aanmaken met default locatie

if (!(Test-Path $savedLastSelectedFolder)){  
write-output $defaultFolder | add-content $savedLastSelectedFolder  
}

# nieuwe folderbrowser object aanmaken

$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog  
$FolderBrowser.RootFolder = “MyComputer”  
#$FolderBrowser.SelectedPath = "C:\VaultWerkmap\Lightronics"  
$FolderBrowser.SelectedPath = Get-Content -Path $savedLastSelectedFolder # lees textfile uit met laatst opgeslagen locatie  
$FolderBrowser.ShowNewFolderButton = “true”  
$FolderBrowser.ShowDialog()

$Prop[‘Folder’].Value = $FolderBrowser.SelectedPath.Replace(“C:\VaultWerkmap\Lightronics",”")

# tekstfile met laatst opgeslagen locatie verwijderen als deze bestaat en nieuwe aanmaken

if (Test-Path $savedLastSelectedFolder){  
Remove-Item $savedLastSelectedFolder  
}  
write-output $FolderBrowser.SelectedPath | add-content $savedLastSelectedFolder  
}[/pre]

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 20, 2019, 6:57am UTC](https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435/7 "2019-11-20T06:57:52Z")

</div>

> [@](#):
>
> … the addin/schript is started again, so it starts as if it is started for the first time? …

That’s what I meant in my first answer. You will have to find a way to determine the folder you want to start the forlder browser dialog in. That should happen **outside** of the function. Then you can use my code example and run the folder browser dialog with the starting folder you like by calling the function with the folder you want provided as parameter.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/folderbrowserdialog-startingat-last-selected-folder/13435/8 "2024-05-16T20:31:02Z")

</div>


