Search code examples
powershellcopy-item

Powershell script to copy and execute


Basically from a input provided network path (e.g "\SERVER\SHARE\") i want to copy a specific Excel file to my desktop and run it automatically. Problem is that on my desktop there are other Excel files containing the letters ZZZ. Any ideas? Thank you!

$title = "Copy ZZZ to Desktop"
$zzzpath = read-host Paste ZZZ path here
copy-item "$zzzpath\*ZZZ*.xlsx" "C:\Users\%username%\Desktop\"
Invoke-Item C:\Users\%username%\Desktop\*ZZZ*.xlsx

Solution

  • You can pipe the output of Copy-Item to your Invoke-Item, forcing the former to pass its output to the latter (via -PassThru):

    $title = "Copy ZZZ to Desktop"
    $zzzpath = Read-Host Paste ZZZ path here
    
    Copy-Item "$zzzpath\*ZZZ*.xlsx" "C:\Users\%username%\Desktop\" -PassThru |
        Invoke-Item