[PowerShell] Drag & Drop files on script


By: Kilavila
April 20, 2020

I have been playing around with drag and drop scripts the last few days, and figured I'd post an how-to.
I'm rather new to PowerShell scripting, so this can probably be done better. But anyway here we go..

This example will compress files and folders to a zip file.

First the PowerShell script:
___________________________________________________________________________________________

$File = $Args[0]

$TimeStamp = Get-Date -Format "yyyy/MM/dd/HH/mm"

# Creating a new file
New-Item -Path "$PSScriptRoot" -Name "$TimeStamp.txt" -ItemType "File"

# Creating a zip file(compressing the txt file)
Compress-Archive -Path $PSScriptRoot\$TimeStamp.txt -DestinationPath $PSScriptRoot\$TimeStamp.zip

# Using a foreach loop: "for each file in argument"
ForEach ($File in $Args) {

# Using the "-Update" parameter to add files to an existing archive
Compress-Archive -Path $File -Update -DestinationPath $PSScriptRoot\$TimeStamp.zip

}

# Then deleting the txt file
Remove-Item -Path "$PSScriptRoot\$TimeStamp.txt" -Force

Start-Sleep -Seconds 3
___________________________________________________________________________________________

Now lets create the shortcut we will be dropping the files on:

Right-Click on Desktop, select "New" > "Shortcut".
Enter this as the target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file C:\YourScriptDirectory\Compress.ps1

Now when you drop files and folder on this shortcut, the files will be passed as the first argument in the script.

You don't have to edit this script, just copy and save it as Compress.ps1..


Hope you learned something new and good luck with your scripting!
Your friendly neighborhood potato Kilavila


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question