1

I have a powershell 2.0 GUI that has a SaveFileDialog box that does not look like windows 7 save file dialog that is used for my other MS Office windows 7 applications - the powershell dialog contains a vertical toolbar, called the Places Bar, and looks like an older version of windows. How can I make the save file dialog in my powershell GUI look like the other windows 7 application save file dialogs?

user1759789
  • 169
  • 2
  • 15

1 Answers1

2

Original post below

Windows will fall back to the old style Windows GUI as you're using PowerShell 2.0, because, by default, PowerShell 2.0 loads .Net 2.0.

$PSVersionTable in PowerShell 2.0

Name                           Value
----                           -----
CLRVersion                     2.0.50727.5485
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

$PSVersionTable in PowerShell 4

Name                           Value                                                                                                                                                                                             
----                           -----                                                                                                                                                                                             
PSVersion                      4.0                                                                                                                                                                                               
WSManStackVersion              3.0                                                                                                                                                                                               
SerializationVersion           1.1.0.1                                                                                                                                                                                           
CLRVersion                     4.0.30319.42000                                                                                                                                                                                   
BuildVersion                   6.3.9600.16406                                                                                                                                                                                    
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                                                                                                              
PSRemotingProtocolVersion      2.2

If you have .net CLR version 3.5 or above installed, you can coerce PowerShell 2.0 into loading a newer CLR version than 2.0, which will then give you the newer style file dialog.

Save the code below into a batch file (e.g. WindowsPowerShell3.5.cmd) where 3.5 is the CLR version you wish to load with PowerShell. Then run the batch file to launch PowerShell. Change powershell.exe to powershell_ise.exe to apply this to a PowerShell ISE session.

@echo off
:: http://stackoverflow.com/questions/7308586/using-batch-echo-with-special-characters
if exist %~dp0powershell.exe.activation_config goto :run
echo.^<?xml version="1.0" encoding="utf-8" ?^>                 > %~dp0powershell.exe.activation_config
echo.^<configuration^>                                        >> %~dp0powershell.exe.activation_config
echo.  ^<startup useLegacyV2RuntimeActivationPolicy="true"^>  >> %~dp0powershell.exe.activation_config
echo.    ^<supportedRuntime version="v3.5"/^>                 >> %~dp0powershell.exe.activation_config
echo.  ^</startup^>                                           >> %~dp0powershell.exe.activation_config
echo.^</configuration^>                                       >> %~dp0powershell.exe.activation_config
:run
:: point COMPLUS_ApplicationMigrationRuntimeActivationConfigPath to the directory that this cmd file lives in
:: and the directory contains a powershell.exe.activation_config file which matches the executable name powershell.exe
set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=%~dp0
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe %*
set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=

Original post

Try the code below, which is based on example code from Microsoft:

WPF

Add-Type -AssemblyName PresentationFramework

$dlg = New-Object 'Microsoft.Win32.SaveFileDialog'
$dlg.FileName = "Document" # Default file name
$dlg.DefaultExt = ".txt" # Default file extension
$dlg.Filter = "Text documents (.txt)|*.txt" # Filter files by extension

# Show save file dialog box
$result = $dlg.ShowDialog()

# Process save file dialog box results
if ($result) {
  # Save document
  $filename = $dlg.FileName;
}

I'm guessing you have code using System.Windows.Forms, similar to the code below:

Windows Forms

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.forms")
$dlg = New-Object System.Windows.Forms.SaveFileDialog
$dlg.FileName = "Document" # Default file name
$dlg.DefaultExt = ".txt" # Default file extension
$dlg.Filter = "Text documents (.txt)|*.txt" # Filter files by extension

# Show save file dialog box
$result = $dlg.ShowDialog()

# Process save file dialog box results
if ($result) {
  # Save document
  $filename = $dlg.FileName
}
TechSpud
  • 3,418
  • 1
  • 27
  • 35
  • Using 3.5 does not work for me - I get an error about .Net: Version v2.0.50727 of the .NET Framework is not installed and it is required to run version 1 of Windows PowerShell. – user1759789 Mar 29 '17 at 14:38
  • Though a PSVersionTable shows that the CLRVersion is 2.0.50727.5485. – user1759789 Mar 29 '17 at 14:43
  • See this SO article on how to tell which versions of .net CLR you have installed: http://stackoverflow.com/a/3495491/1368849 – TechSpud Mar 29 '17 at 14:48
  • according to my registry it shows v2.0.50727, v3.0, v3.5, and v4. All of which give the same .net version error. – user1759789 Mar 29 '17 at 15:09
  • See a possible solution in this link I found when I googled your error: http://mikefrobbins.com/2012/07/24/error-when-running-powershell-version-2-on-windows-8-and-windows-server-2012/ – TechSpud Mar 29 '17 at 15:14
  • I'll read the link, but after a quick glance, .NET 3.5 is fully enabled on my windows 7 machine already. – user1759789 Mar 29 '17 at 15:18
  • But you are right I did not google the error - I will do that now! – user1759789 Mar 29 '17 at 15:20
  • Sorry! I amended my comment about not googling - it was too harsh. I think the Mike F Robbins link is on the right traack though. – TechSpud Mar 29 '17 at 15:22