# encoding: ascii
# api: csharp
# title: Modal File Dialogs
# description: There are problems with displaying modal dialogs from PowerShell in XP SP3. When the ShowDialog() method is called, the dialog is not modal and is behind the PowerShell window. To solve this problem, you need to add a class that implements System.Windows.Forms.IWin32Window and instantiate that class with the handle to the main window of the running process, and then pass that handle as a parameter to the ShowDialog() method to make the dialog act modally.
# version: 0.1
# type: class
# author: George Howarth
# license: CC0
# x-poshcode-id: 2002
# x-archived: 2017-03-25T01:51:40
# x-published: 2011-07-22T02:41:00
#
#
Add-Type -TypeDefinition @"
using System;
using System.Windows.Forms;

public class Win32Window : IWin32Window
{
    private IntPtr _hWnd;
    
    public Win32Window(IntPtr handle)
    {
        _hWnd = handle;
    }

    public IntPtr Handle
    {
        get { return _hWnd; }
    }
}
"@ -ReferencedAssemblies "System.Windows.Forms.dll"

$owner = New-Object Win32Window -ArgumentList ([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.ShowDialog($owner)
