Cannot echo environment variable with space after storing in variable [duplicate] - powershell

This question already has answers here:
Return value of environment variable with powershell
(2 answers)
Closed 4 years ago.
Using Powershell prompt, I cannot seem to echo a system environment variable that has a space in it, stored in a variable.
For instance, if I do a simple:
echo ${env:My Var}
I get back the value of that environment variable - which is the word "test". That works great.
Then, if I echo a preset variable that is set to the string "My Var", I get back what I'd expect, the word "My Var".
echo $variable
If I then echo a hybrid of the two:
echo env:${variable}
I also get back what I'd expect, which is the string value "env:My Var".
But now I want to use that $variable to output the value of the corresponding environment variable. Meaning, I want to use something like:
echo ${env:${variable}}
... and get back the value of the environment variable, the word "test".
But that doesn't work. I get a red error saying Use `{ instead of { in variable names. But nothing like that works either.
Any help would be appreciated.

this worked for me:
cd env:
new-item 'test 123' -Value "123"
${env:test 123}
123
$var = 'test 123'
(Get-item env:$var).value
123
(Get-item env:$var).name
Test 123

Related

PowerShell - Merge two variables into one

I'm learning PowerShell so please forgive (what I'm sure is) a simple question.
I'm used to coding BATCH scripts and if I wanted to merge %USERDOMAIN% and %USERNAME% I would:
set zFullUsername=%USERDOMAIN%\%USERNAME%
echo %zFullUsername%
How can I do the same in PowerShell?
Thank you for your time.
On a supported Operating System, I wouldn't even bother with environment variables for this:
$zFullUsername = whoami
Then just access it as required:
$zFullUsername
In PowerShell, you can access environment variables in a few different ways. The way I recommend is to use the $env:VAR variable to access them.
$user = $env:USERNAME
$domain = $env:USERDOMAIN
echo "$domain\$user"
Note: \ is not an escape character in the PowerShell parser, ` is.
Similarly to rendering the echo command (echo is an alias of Write-Output btw) you can create a username variable like so:
$fullUserName = "$domain\$user"
Or you can skip right to creating $fullUserName straight from the environment variables:
$fullUserName = "${env:USERDOMAIN}\${env:USERNAME}"
Note: When variables have non-alphanumeric characters in them, the ${} sequence tells PowerShell everything between the ${} is part of the variable name to expand.
It seems the : in $env:VAR is actually an exception to this rule, as"Username: $env:USERNAME" does render correctly. So the ${} sequence above is optional.
To avoid confusion when trying to apply this answer in other areas, if you needed to insert the value of an object property or some other expression within a string itself, you would use a sub-expression within the string instead, which is the $() sequence:
$someVar = "Name: $($someObject.Name)"
When using either ${} or $(), whitespace is not allowed to pad the outer {} or ().

Powershell can't access Object attributes field and values

I'm working on to get the cmd command output in Powershell. The cmd command I ran as follows.
$obj = cmd.exe /c 'twiddle -s localhost -u xyz -p abc get "service=tempservice" stats'
If I print the object it prints the value as
Stats=[Ljava.lang.String;#c43b53
I want to access the value of this string object in PowerShell so that I can have all the fields and values of this $obj. I'm looking to get the value of one attribute and field named "count=1020132" inside the stats.
My Get-Member doesn't have any property or method which can help me in that.
I tried doing $obj."property" but didn't help.
I would prefer to do it with regex using named capture groups.
Please be so kind and adapt the regular expression to your needs, the used one is only an example.
$str ='Stata=[Ljava.lang.String;#c43b53';
[regex]$rx = "(?<trailer>\S+)\=\[(?<first>\S+)\.(?<second>\S+)\.(?<third>\S+);(?<leader>\S+)";
$m = $rx.Match($str);
$m;
$m.Groups["trailer"].Value;
$m.Groups["first"].Value;
$m.Groups["second"].Value;
$m.Groups["third"].Value;
$m.Groups["leader"].Value;

Set variable in a single PowerShell line

Can any of you let me know how to state a variable in a PowerShell line? Just like I could do using PowerShell ISE I would like to be able to do so via the plain console.
You can assign values to a PowerShell variable by combining a variable name, an assignment operator, and an expression. Here is a simple example:
>> $a = 1 + 1
If you want to populate multiple variables with the same value, you can save some typing as in the example below:
>> $a = $b = $c = 1
You can also define multiple variables with different values on one line:
>> $a, $b, $c = 1, 2, 3
To display the value of a variable, you don’t need a special command as in many other programming languages; entering the variable name is enough. This works in a script and on a command prompt.
>> $c
To take values from user, similar to Python input() function you can use:
$Number = Read-Host "Please enter a number"
Read the official documentation here.
I finally found the solution to get the job done. I just set the variable and end it with a semi-colon. Then I can go on with scripting.
Thank you all.

Batch file to set a PowerShell variable

For some reason i simply can't understand most of the sites who explain this question. So i'll try to ask here, if i'm am in the wrong place, just tell me in the comments and i'll put this in another forum and delete this question.
Let's say that i have 2 files, Batch.bat and PowerShell.ps1.
Batch.bat:
set A="ThisIsSuchVar!"
PowerShell.ps1:
$B = "Well, i don't know what to do here"
What can i do to the B variable be the same as the A variable?
Remember: I want the Batch variable to go to the PowerShell file. It's an one-way script. I want to use the built-in windows sources. And please, consider that i am a complete newbie in programming and don't speak english very well, so be the simplest possible, please.
In your batch file run.bat, set the environment variable A and run the PowerShell script:
set A=8
PowerShell.exe -File .\script.ps1
pause
In script.ps1, get the environment variable A, and assign its value to B:
$B=$Env:A
echo $B
When you run run.bat you get:
C:\Temp\try>set A=8
C:\Temp\try>PowerShell.exe -File .\script.ps1
8
C:\Temp\try>pause
Press any key to continue . . .

Cannot resolve environment variables in Powershell with periods "." in them

I am trying to access environment variables using Powershell. The environment variables, which is out of my control, contain periods in this format:
ENVIRONMENT_VARIABLE.1
Therefor, upon assigning the value of the environment variable to a local variable, it looks like this:
$myvar = $env:ENVIRONMENT_VARIABLE.1
However, every time I try to retrieve this variable, it always leaves out the ".1", returning nothing.
I'm aware periods + Bash = no bueno, but again, this is out of my control and I need a work around. If it helps, I already tried this as well with no luck:
$myvar = ${env:ENVIRONMENT_VARIABLE.1}
I appreciate any and all help on this. Thanks!
The last format you're using works for me:
PS> ls env:\fo*
Name Value
---- -----
Foo.1 bar.1
PS> ${env:foo.1}
bar.1