How can I add parameters in Powershell to change file destination and version number

Viewed 84

Edit: I have now replaced the user with "$env:username" to correct the first part. Would anyone know how to know add a prompt before running the code to allow a user to type nothing, "A", "B", "C" to replace (version) in the code on the first line?

I completed a code in Powershell to copy files from one destination to the other. I am hoping to make it so when running script, the user can enter their alias and it will replace "alias" in the script. The second thing would be for the user to enter "A" "B" "C" or nothing in order to replace "(version)" in the script.

#Create New Folders 

new-item -path "C:\Users\$env:username\Archive" -name "TeamsCopy$((get-date).tostring("MMdd"))(version)" -itemtype "directory"

new-item -path "C:\Users\$env:username\Archive\TeamsCopy$((get-date).tostring("MMdd"))\Canada" -itemtype "directory"
 
 
#Copy Files 

Copy-item -path "C:\Users\$env:username\SourceFolder\Canada.xlsx" -destination "C:\Users\alias\Archive\TeamsCopy$((get-date).tostring("MMdd"))\Canada"

Does anyone have any idea how to add this in as a prompt and then also the right syntax to use?

1 Answers

The simplest way would be Read-Host

$version = Read-Host "Enter the version (A, B, C or none)"

But this bears the risk of invalid user input. Alternatively, you chould prompt for a choice from a fixed list:

$title = $null
$prompt = "Choose the version:"
$choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&A", "&B", "&C", "&None")
$default = 3
$choice = $Host.UI.PromptForChoice($title, $prompt, $choices, $default)
$version = ("A","B","C","")[$choice]

And then in your New-Item command:

-Name "TeamsCopy$((Get-Date).ToString("MMdd"))($version)"
# or
-Name ("TeamsCopy{0:MMdd}({1})" -f (Get-Date), $version)
Related