title: How to use OpenFileDialog to go to specific folder using Powershell?
body: <p>I have an GUI to select a file. I use OpenFileDialog. But when I press the button to open the file, the target folder sometime different. I want to make it default folder when I click the button.</p>
  <pre><code>Function Sel_File($InitialDirectory)
  {
      Add-Type -AssemblyName System.Windows.Forms
      $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
      $OpenFileDialog.Title = &quot;Please Select File&quot;
      $OpenFileDialog.InitialDirectory = $InitialDirectory
      $OpenFileDialog.filter = “All files (*.*)| *.*”
      If ($OpenFileDialog.ShowDialog() -eq &quot;Cancel&quot;) 
      {
      [System.Windows.Forms.MessageBox]::Show(&quot;No File Selected. Please select a file !&quot;, &quot;Error&quot;, 0, 
      [System.Windows.Forms.MessageBoxIcon]::Exclamation)
      }   $Global:SelectedFile = $OpenFileDialog.SafeFileName
      
  }
  Add-Type -AssemblyName System.Windows.Forms
  [System.Windows.Forms.Application]::EnableVisualStyles()
  
  $Form                            = New-Object system.Windows.Forms.Form
  $Form.AutoSize                   = $true
  $Form.text                       = &quot;Auto GM Creation&quot;
  $Form.TopMost                    = $true
  #----------------------
  
  $ChooseML_L                      = New-Object system.Windows.Forms.Label
  $ChooseML_L.text                 = &quot;MLs&quot;
  $ChooseML_L.AutoSize             = $true
  $ChooseML_L.width                = 25
  $ChooseML_L.height               = 10
  $ChooseML_L.location             = New-Object System.Drawing.Point(28,20)
  $ChooseML_L.ForeColor            = &quot;#000000&quot;
  
  $SelectML                        = New-Object system.Windows.Forms.TextBox
  $SelectML.AutoSize               = $true
  $SelectML.width                  = 150
  $SelectML.height                 = 30
  $SelectML.location               = New-Object System.Drawing.Point(120,40)
  $SelectML.Text                   = &quot;Selected ML&quot;
  
  $ChooseML                        = New-Object System.Windows.Forms.Button
  $ChooseML.text                   = &quot;Select File&quot;
  $ChooseML.AutoSize               = $true
  $ChooseML.width                  = 90
  $ChooseML.height                 = 20
  $ChooseML.location               = New-Object System.Drawing.Point(28,38)
  $ChooseML.ForeColor              = &quot;#ffffff&quot;
  $ChooseML.BackColor              = &quot;#093c76&quot;
  
  $ChooseML.Add_Click({Sel_File
  $SelectML.Text = $Global:SelectedFile
  }) 
  
  #----------
  $Apply                         = New-Object system.Windows.Forms.Button
  $Apply.BackColor               = &quot;#6996c8&quot;
  $Apply.text                    = &quot;Apply&quot;
  $Apply.width                   = 99
  $Apply.height                  = 30
  $Apply.location                = New-Object System.Drawing.Point(320,190)
  
  #----------
  $Cancel                         = New-Object system.Windows.Forms.Button
  $Cancel.BackColor               = &quot;#6996c8&quot;
  $Cancel.text                    = &quot;Cancel&quot;
  $Cancel.width                   = 98
  $Cancel.height                  = 30
  $Cancel.location                = New-Object System.Drawing.Point(450,190)
  $Cancel.Add_Click({$Form.Close()})
  
  #-----------
  
  $Prefix                      = New-Object system.Windows.Forms.Label
  $Prefix.text                 = &quot;Prefix&quot;
  $Prefix.AutoSize             = $true
  $Prefix.width                = 25
  $Prefix.height               = 10
  $Prefix.location             = New-Object System.Drawing.Point(28,80)
  $Prefix.ForeColor            = &quot;#000000&quot;
  
  $NB                              = New-Object system.Windows.Forms.RadioButton
  $NB.text                         = &quot;NB&quot;
  $NB.AutoSize                     = $true
  $NB.BackColor                    = &quot;#4a90e2&quot;
  $NB.width                        = 104
  $NB.height                       = 20
  $NB.location                     = New-Object System.Drawing.Point(28,100)
  
  $DPC                             = New-Object system.Windows.Forms.RadioButton
  $DPC.text                        = &quot;DPC&quot;
  $DPC.AutoSize                    = $true
  $DPC.BackColor                   = &quot;#4a90e2&quot;
  $DPC.width                       = 104
  $DPC.height                      = 20
  $DPC.location                    = New-Object System.Drawing.Point(100,100)
  
  $Form.Controls.AddRange(@($ChooseML, $Prefix, $ChooseML_L, $Apply, $Cancel, $SelectML, $NB, $DPC))
  [void] $Form.ShowDialog()
  </code></pre>
  <p>With this code I can go to any folder.
  My expectation I can go to a specific folder that I need and choose the file.</p>
  <h2>Updated</h2>
  <p>This is working</p>
  <pre><code>Function Sel_File
   {
   $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
   $OpenFileDialog.InitialDirectory = “C:\Users\XX”
   $OpenFileDialog.Title = &quot;Please Select File&quot;
   $OpenFileDialog.filter = “All files (*.*)| *.*”
   If ($OpenFileDialog.ShowDialog() -eq &quot;Cancel&quot;) 
   {
    [System.Windows.Forms.MessageBox]::Show(&quot;No File Selected. Please select a file !&quot;, &quot;Error&quot;, 0, 
    [System.Windows.Forms.MessageBoxIcon]::Exclamation)
    }   $Global:SelectedFile = $OpenFileDialog.SafeFileName
      
  }
  </code></pre>