Have File Explorer Current working Directory Passed to PowerShell Script
Asked Answered
C

1

1

Good Afternoon All!

Working on a script that my co-workers use, we move a lot up to Sharepoint and the objective was to make an easy method to set the file to read-only and compress it using the built-in NTFS compression.

All of that is functioning like it should with the code below. The problem I need to solve is this, somehow I need to pass the current directory the worker has open in File explorer to The open file dialog box. So that when the script is run you don't have to manually change it to the correct location.

Currently, I have it set to open on the users desktop directory

Is this possible?

Thanks

Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    InitialDirectory = [Environment]::GetFolderPath('Desktop')
    Multiselect = $true # Multiple files can be chosen

}

[void]$FileBrowser.ShowDialog()

$path = $FileBrowser.FileNames;

If($FileBrowser.FileNames -like "*\*") {

    # Do something before work on individual files commences
    $FileBrowser.FileNames #Lists selected files (optional)

    foreach($file in Get-ChildItem $path){
    Get-ChildItem ($file) |
        ForEach-Object {
            Set-ItemProperty -Path $path -Name IsReadOnly -Value $true
            compact /C $_.FullName
        }
    }
    # Do something when work on individual files is complete
}

else {
    Write-Host "Cancelled by user"
}
Concertmaster answered 17/7, 2019 at 19:46 Comment(0)
O
4

The following solution determines the directory that the topmost File Explorer window has open (the window that is highest in the Z-order).

That is, if File Explorer itself is active, that active window's directory is used, otherwise it is the most recently active window's.

# Helper type for getting windows by class name.
Add-Type -Namespace Util -Name WinApi  -MemberDefinition @'
  // Find a window by class name and optionally also title.
  // The TOPMOST matching window (in terms of Z-order) is returned.
  // IMPORTANT: To not search for a title, pass [NullString]::Value, not $null, to lpWindowName
  [DllImport("user32.dll")]
  public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
'@

# Get the topmost File Explorer window, by its class name.
$hwndTopMostFileExplorer = [Util.WinApi]::FindWindow(
  "CabinetWClass",     # the window class of interest
  [NullString]::Value  # no window title to search for
)

if (-not $hwndTopMostFileExplorer) {
  Write-Warning "There is no open File Explorer window."
  # Alternatively, use a *default* directory in this case.
  return
}

# Using a Shell.Application COM object, locate the window by its hWnd and query its location.
$fileExplorerWin = (New-Object -ComObject Shell.Application).Windows() |
                     Where-Object hwnd -eq $hwndTopMostFileExplorer

# This should normally not happen.
if (-not $fileExplorerWin) {
  Write-Warning "The topmost File Explorer window, $hwndTopMostFileExplorer, must have just closed."
  return
}

# Determine the window's active directory (folder) path.
$fileExplorerDir = $fileExplorerWin.Document.Folder.Self.Path

# Now you can use $fileExplorerDir to initialize the dialog:
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    InitialDirectory = $fileExplorerDir
    Multiselect = $true # Multiple files can be chosen
}

$null = $FileBrowser.ShowDialog()

# ...
Oleaster answered 21/7, 2019 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.