Goal
1. Change the name of a file through drag and drop.
2. Create a script shortcut to make script shortcuts to a script but with different arguments. (Like a button that makes other buttons that do slightly different things when pressed.)
Utility: To quickly execute commonly used scripts or scripts with different arguments.
Prepend Tag Script with Arguments
Drag a file to the shortcut (of a script) to rename it (here, short text is just added in front). The shortcut itself defines the “tag” string that would be added filename of the file.
Input: File via drag and drop. Result: File with only the filename modified.
prepend-tag-z.ps1
param ( # parameter definitions
$tagString,
$filePath # later: could handle missing/extra args?
)
$showOutput = $true
if ($showOutput) {
if ($tagString){
Write-output ("`$tagString: " + $tagString) # string to prepend to filename, defined in the shortcut target.
}
if ($filePath){
Write-output ("`$filePath: " + $filePath) # path to file drag+dropped to the script shortcut.
}
Write-output "---- -----"
}
$fileNameWithExt = ([io.path]::GetFileName($filePath)) # original filename with extension.
$newNameWithExt = ($tagString + $fileNameWithExt)
rename-item -LiteralPath $filePath $newNameWithExt # renames file with new name.
if ($showOutput) {
Write-Output $tagString
Write-Output $fileNameWithExt
Write-Output $newNameWithExt
Write-output "---- ----- ---- ----"
Read-Host -Prompt "Press Enter to exit"
}
Shortcut Target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -file "<path to .ps1 script>\prepend-tag-z.ps1" "aa bb, "
"aa bb," is the custom string that would be added to filename.
The text would need quotation marks if the text has a space character. Quotation marks also helps identify that it is a string.
Powershell flags: reference: learn.microsoft.com powershell 5.1-ExecutionPolicy RemoteSigned : sets execution policy. On a windows desktop, it’s Restricted by default. RemoteSigned allows scripts created locally to run, but requires digital signatures from downloaded scripts for added security.
The following script creates shortcuts like these with the tag string defined by what is drag+dropped to it.
Script to create Shortcuts with Arguments
Dragging a file to a shortcut of this script would create a shortcut to a different script with the filename of the file that was dropped as an argument. More simply, create a script shortcut with a filename as the argument.
Input: File where only the filename is used via Drag & Drop. Output: Creates a script shortcut with the File's filename as an argument and the created shortcut's name.
create-prepend-tag-z.ps1
This code will need the file path of the script above (into $scriptPath) to function correctly.
param ( # argument definition
$filePath
)
$showOutput = $true
if ($showOutput) {
Write-output "POWERSHELL SCRIPT | filename: create-prepend-tag-z.ps1"
Write-output "updated: 24.05.16"
Write-output "the shortcut created adds ', ' search and find tagArgumentPoint in this script to change."
Write-output "the tagging script only prepends the argument given and does nothing else."
Write-output ("`$filePath: " + $filePath)
Write-output "---- -----"
}
$fileName = ([io.path]::GetFileNameWithoutExtension($filePath))
$scriptPath = "[copy the path to the tagging script (.ps1) here.]\prepend-tag-z.ps1"
$shortcutName = ($fileName + ",.lnk")
# create shortcut if not created previously.
if (-not(Test-Path -Path $shortcutName -PathType Leaf)) {
$WshShell = New-Object -comObject WScript.Shell # used to create a shortcut file.
$Shortcut = $WshShell.CreateShortcut($shortcutName)
$targetValue =
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$cmdArgs =
('-ExecutionPolicy RemoteSigned -file "'+ $scriptPath +
'" "'+ $fileName +', "') # tagArgumentPoint
# $fileName is the text that will be prepended to the filename when tagging script is executed.
$Shortcut.TargetPath = $targetValue
$Shortcut.Arguments = $cmdArgs
$Shortcut.Save()
Write-Output "Full Shortcut Target Value:"
Write-Output ($Shortcut.TargetPath +" "+ $cmdArgs)
Write-output ($shortcutName +" was made.")
}else {
Write-output ($shortcutName +" exists.")
}
if ($showOutput) {
Write-output "---- ----- ---- ----"
}
Read-Host -Prompt "Press Enter to exit"
Shortcut Target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -file "<path to .ps1 script>\create-prepend-tag-z.ps1"
Start in: Should be empty, clear it if not. This makes the new shortcut be made in the same location as this script shortcut.
Side Notes
- These scripts can be modified so that it removes certain text from the filename instead.
- Scripts could try to handle cases of character limits, missing/extra/mismatching arguments.
- Script could be updated to handle multiple arguments instead of only 1.
- The Back tick character ,
`, can be used escape special characters in strings.
An error such as with a line like Unable to save shortcut "C:\Windows\System32\WindowsPowerShell\v1.0\aa,.lnk". means that the Start in: field of the shortcut of the second script was not made empty.
An older version of this code had extra lines that it did not need:
- Reviewed these extra lines because of the notes from the answer made by Lewis Newton here: https://stackoverflow.com/a/67655089
CmdletBinding(): enables access to special parameters, but these scripts do not use them.[ValueFromRemainingArguments=$true]. ValueFromRemainingArguments can probably be used to handle if multiple files are passed with drag and drop.- CmdletBinding() : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_cmdletbindingattribute?view=powershell-7.4
- [ValueFromRemainingArguments=$true] : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-5.1#valuefromremainingarguments-argument

