I have a batch file that ask the user for a variable line set /p asset=
. Im calling my powershell script like this
SET ThisScripts
If you have a file.ps1
that takes a parameter,
param($asset)
"The asset tag is $asset"
You can pass in the variable as an argument.
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%file.ps1
SET /p asset=Enter the asset name
PowerShell -NoProfile -ExecutionPolicy Bypass -file "%PowerShellScriptPath%" "%asset%"
param($asset)
This has to be the very first line in the PowerShell script for it to work, else it will fail.
You can use $env:variable_name
syntax to access curent cmd environment variables from powershell. To get hold of your variable you'd use $env:asset
To try, open cmd
, do set "myvar=my value"
, start powershell
, do $env:myvar
(this will simply print it, but of course you can use it as any other ps variable)
Just as a sidenote, ps has good help system. If you do help env
it will list two relevant topics which you can examine in turn to get detailed information.
Hope this helps