Forum Discussion
balubeto
Nov 01, 2022Brass Contributor
Saving an input to an variable
Hi How do I save an input given by me to a variable? For example, when I run a script, which contains the Test-Path command, the Terminal window asks me to insert a path. How do I save this ...
oliwer_sundgren
Nov 06, 2022Iron Contributor
Hello! balubeto
You can add a variable to your code by using a dollar sign and then a name for the variable, this will then be followed by an equals sign and the value of the variable.
See an example below
$PathVariable = "C:\Users\User1\Temp\"
Test-Path -Path $PathVariable
You could also make the script more interactive by letting the script ask for input by the user and then saving that input to a variable to then later be used in the script. See example below
$FilePath = Read-Host "Please enter a filepath to test"
Test-Path -Path $FilePath
Did this answer your question? Otherwise please let me know and I'd love to help you further 🙂
Kind Regards
Oliwer Sundgren