1

I am trying to accomplish the following:

  1. Open a Windows File Explorer and navigate to a particular folder.
  2. Select certain files and/or folders within that folder using the mouse.
  3. Write a powershell script that detects the files/folders I have selected, and copies to another location.

What I need to know, is there a way for powershell to detect the files I have selected in the File Explorer? I have tried to locate resources online to give me some insight, but no luck, either it isn't possible or I am searching the wrong terminology.

Laslos
  • 340
  • 1
  • 7
  • 19
  • 2
    Why would you accomplish this? Most likely it makes more sense to use an OpenFileDialog. – vrdse Feb 26 '18 at 18:20
  • I am working on a large website not under version control, trying to write this script to copy certain files and/or folders I have made changes to, to another folder to diff the test site with the live site - could be anywhere from a few files to a few hundred. Currently I am manually copying and pasting these into a separate folder to diff them, looking for a way to automate the process. – Laslos Feb 26 '18 at 18:27
  • I will try the `OpenFileDialog` - seems like it may meet my needs. – Laslos Feb 26 '18 at 18:35
  • 1
    I also think that the OpenFileDialog control (which you can use in powershell) is probably the best bet. Having said that you can use it with Out-Gridview also. – EBGreen Feb 26 '18 at 18:35
  • 1
    You may want to look at this answer: https://stackoverflow.com/questions/15932881/invoking-powershell-command-from-windows-explorer-send-to-menu#15934981 – TToni Feb 26 '18 at 18:42
  • @vrdse thanks, `OpenFileDialog` was exactly what I needed, and gives me more flexibility than what I was looking for. I think I was looking at it the wrong way, probably bias from my current manual process. – Laslos Feb 26 '18 at 18:48
  • @TToni that looks to be the functionality I was initially looking for, thanks for linking it. Probably going to go with the `OpenFileDialog` though, will allow me to skip the File System altogether, and support multiple websites as well. – Laslos Feb 26 '18 at 18:50

3 Answers3

1

Most likely it makes more sense to use an OpenFileDialog.

vrdse
  • 2,899
  • 10
  • 20
1

Something like this (keep CTRL key pressed and select your files)

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = "c:\temp"
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.Multiselect=$true
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.FileNames | Copy-Item -Destination "C:\tempdest"
$OpenFileDialog.Dispose()
Esperento57
  • 16,521
  • 3
  • 39
  • 45
1

Or without explorer (then you can navigate, but if you know your directory its an other method) :

Get-ChildItem "c:\temp" -file | 
    Out-GridView -Title "Select your files" -OutputMode Multiple | 
        Copy-Item -Destination "C:\tempdest"
Esperento57
  • 16,521
  • 3
  • 39
  • 45