user1759789
user1759789

Reputation: 189

Powershell 2.0 Savefiledialog style

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?

Upvotes: 2

Views: 2662

Answers (1)

TechSpud
TechSpud

Reputation: 3528

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
}

Upvotes: 3

Related Questions