Powershell Application: Drag and Drop to pass Arguments

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:


PowerShell Script: Note Token Generator

Description

The purpose of this script is to create special “note token” files which are generally used to make small notes. My nickname for these are Pico Notes, for personal reasons.

Reserved characters for filenames:

  • It’s an image so you can put it where you can reference it easily.
  • A square image is also easy to see as a thumbnail.

Value/Utility/Hypothesis/Design/Theory

  • Simple to use, no extra programs aside from the small simple script below.
  • Note Tokens supposedly carry more mental weight than standard notes.
  • Exist on the same “plane” of your work area.
  • Don’t have to open the file to read it, may save time.
  • Gets you thinking about notes in a different way. Similar to rewiring your brain and looking at things at a different perspective. Because taking notes is a basic step, it could help you think about other basic things differently.
  • Notes are purely stored in the filename and the other contents of the file should be left empty.
  • The extension, “.ntok”, is mostly cosmetic other than to identify note token files.
  • Organize and track tasks.
  • Can spread the concepts to file folders, url files, and images. Concept is more manageable when it’s just Meta information and not the actual data.

Background Info

I usually just copy a of a shortcut that executes the code into the folder that I want to make Note Tokens in. I’m thinking there could be a more efficient way to execute this code via a keyboard shortcut. The word “generator” is used because it implies that this is used to create multiple things instead of one thing like a general verb word like “make” though using the verb form, “generate” more of a standard naming procedure. (nomenclature/jargon)

To-do List

Link to post on making executing scripts with a shortcut file. Link to post about going into detail about Note Tokens. Create separate post about Note Tokens, post should only be about Note Tokens or include thoughts about note taking. (Internal Note: Vocab: cmdlet)

# version 1.02
# version date: 22.04.23

<#
== references ==
get-date reference
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-7.2

== date formats ==
(get-date -format "yy.MM.dd")
example: 22.04.23
(get-date -format "HH.mm")
example: 23.59
- this is 24 hour format.

== parent folder name ==
$parentFolder = Split-Path -Path (Get-Location) -Leaf

#>
$priorityPrefix = "zz" ## Generally use "zz" or "aa"
$name = -join($priorityPrefix, "--d",(get-date -format "yy.MM.dd"),"-t24-",(get-date -format "HH.mm") )
# zz--d22.04.16-t24-09.06.ntok

New-Item -Path .\$name.ntok
# no new token made if same name exists.

# read-only to reduce overhead.
Set-ItemProperty -Path .\$name.ntok -Name IsReadOnly -Value $true

<#
== notes ==
ntok - abbreviation of note token.
#>

<#
== change log ==
- 22.04.25 - 1.04 - new-item reference link, read only setting. comment out to disable.
- 22.04.23 - 1.02 - cleanup, documentation of code bits.
- 22.04.15 - 1.00 - initial
1.01 date stamp
1.02 timestamp
#>

Reg Ex: Substrings (ex: $1, $2)

I’ve recently had to learn about these substring identifiers as they are used in powershell scripts and the program, Bulk Rename Utility.

These are useful in separating a string into substrings, as well as keeping substrings as needed.


If I’m following the information below correctly:
MDN web docs: RegExp.$1-$9

Here is an example of substring matching with a global RegEx Object.

example regex: /(a\w+)\s(b\w+)/
example string: zebra alpha beta yankee
regex.$1 = alpha
regex.$2 = beta

The strings zebra and yankee aren’t matched and are basically ignored.


Using the same example string and a different regex below:

example regex: /(\w+)\s(\w+)/
regex.$1 = zebra
regex.$2 = alpha

Here, as the regex isn’t global, it only returns the first match, beta and yankee and ignored.


Replace in VS Code but keep existing substring

VS code allows the usage of using regular expressions to find and, optionally, replace text.

vscode substring

Would take the text in the following block:

1:
2:
3:

And replace matched instances while keeping matched substrings, like in the block below.

1
|-
|
*
2
|-
|
*
3
|-
|
*

This text example was used to organize information/data in a wiki table.

Writing a Powershell Script: Rename Files

Goal: Rename a set of files using a powershell script.

Example

start: alpha bravo charlie delta.png
change: Remove “charlie .*”
end: alpha bravo.png
  • Learn powershell and it’s syntax.
  • Execute a powershell script by double clicking.
  • Show executed code for bug checking.
  • Functions used: rename-item, replace()

Solution

Script Code:

get-childitem *.png | foreach { rename-item -LiteralPath $_ ($_.Name -replace “( charlie .+)”, “$1.png”) }

Edit (5/14/2024): -LiteralPath is a flag for rename-item which takes the second argument, $_ (the path of the current file), exactly as it is. This flag is necessary because filenames could contain special characters like “[” which are treated differently if left un-escaped or not taken into account.

This script loops through all png files and renames them sometimes with the same file name.

An issue with the script is that renames all png files instead of png files that match the condition. It seems probable that this code can be updated to be as selective.

For the shortcut:
Target:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command “& ‘[path to ps1 file]\script-name.ps1’
Start in: was left blank.

Steps to find this solution

First googled for an answer, initially looked at doing this via a command line batch script but did not find a similar solution doing this.

Found these answers which I combined and ran until it worked correctly.
(Super User: How do I remove the same part of a file name for many files in Windows 8?)
(Stack Overflow: how do I rename files in Powershell with regex?)

Reference on commands I used.
(SS64: Rename-Item)
(SS64: Replace())
(SS64: Regular Expressions)

Usage of the “-replace” option (regular expressions)
(Microsoft | Powershell: About Comparison Operators)

Running the script by double clicking a copy of a shortcut.
(Stack Overflow: Is there a way to make a PowerShell script work by double clicking a .ps1 file?)

Keeping the window open and showing execution/errors.
(Microsoft | PowerShell: Write-Output)
(Stack Overflow: How to keep the shell window open after running a PowerShell script?)
(Microsoft: Set-PSDebug)
(Server Fault: PowerShell script, showing commands run)

Filtering a collection.
(Concurrency: PowerShell Basics – Filtering and Selecting by Mitchell Grande)