Forum Discussion

balubeto's avatar
balubeto
Brass Contributor
Nov 01, 2022

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 path to a variable?

 

Thanks

 

Bye

  • You can insert a $path='c:\temp' at the beginning of the script and use the Test-Path -path $path to test it.
  • 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

Resources