Write-Host "
assoc.Id) -
assoc.Name) -
assoc.Owner)"
See the Windows PowerShell Language Specification Version 3.0, p25, sub-expressions expansion.
Answer from David Brabant on Stack OverflowI am hoping for some assistance on using a variable inside a string. Here is what is should look like
$Variable = 20 $GrouptoAdd = "SG_VPN-"$Variable"_User"
I cant seem to get powershell to interpret it correctly.
What I want it to look like
SG_VPN-Site-20_User
Write-Host "
assoc.Id) -
assoc.Name) -
assoc.Owner)"
See the Windows PowerShell Language Specification Version 3.0, p25, sub-expressions expansion.
There is a difference between single and double quotes. (I am using PowerShell 4).
You can do this (as Benjamin said):
$name = 'Slim Shady'
Write-Host 'My name is'$name
-> My name is Slim Shady
Or you can do this:
$name = 'Slim Shady'
Write-Host "My name is $name"
-> My name is Slim Shady
The single quotes are for literal, output the string exactly like this, please. The double quotes are for when you want some pre-processing done (such as variables, special characters, etc.)
So:
$name = "Marshall Bruce Mathers III"
Write-Host "$name"
-> Marshall Bruce Mathers III
Whereas:
$name = "Marshall Bruce Mathers III"
Write-Host '$name'
-> $name
(I find How-to: Escape characters, Delimiters and Quotes good for reference).
How to pass the string from variable instead using it as parameter
powershell - Setting a (string) variable equal to another (string) variable including modifications - Stack Overflow
How to set a variable in cmd which is a string from powershell command result? - Stack Overflow
Using Variable inside a string
When defining variables in PowerShell, single quotes (') mean you want the literal version of the string. Use double-quotes (") if you want to allow variable expansion:
PS C:\> $a = "hello"
PS C:\> $a
hello
PS C:\>
a world!"
PS C:\> $b
hello world!
More info:
- Single Quotes vs. Double Quotes in PowerShell: What's the Difference?
- Referencing Variables and Variable Values
Edit after comments:
For your example where you're pulling the line from a file, that's a little trickier since it's pulling the line as a literal string.
The easiest way (IMO) would be to use the Replace method; something like:
$selHost = (get-content c:\scripts\hosts.txt)[0]
$a = ((get-content c:\scripts\config.txt)[1]).replace('$selhost', $selHost)
When you read a string that contains a variable name out of a file and into a variable, you are going to need something other than double quotes to cause string expansion. There is a tool that's relevant here. It's called ExpandString. Take a look at this sample code:
$selhost = 'spr-it-minion'
selhost is offline!'
ExecutionContext.InvokeCommand.ExpandString(
c
What's going on here is that first I have given $selhost and $b literal values, similar to the ones you would read out of the files you are using. Of course, $b isn't right, because the reference to $selhost isn't resolved, as outlined in the accepted answer. But $c gets the value produced by expanding $selhost, which you can use in your output.
I'll leave applying this to your case as a coding exercise.
In PowerShell any output can be assigned to a variable. If it isn't assigned or otherwise consumed it will output to the host, usually the console.
Your example derived from pseudo code might be something like:
Copy$SomeOtherVar = $SomeVar -replace "one", "two"
The same would be true if you invoked a .Net method on the string:
Copy$SomeOtherVar = $SomeVar.Replace( "one", "two" )
Also important is assigning the output of a command which can be a cmdlet, function or even a command line executable.
Note: that calling functions & cmdlets is a little different in PowerShell. Don't specify arguments parenthetically. Separate parameters and arguments with spaces and/or use the named parameters.
Copy$SomeOtherVar = Get-SomeData $SomeVar
$SomeOtherVar = Ping $SomeVar
The summary answer to your question is anything PowerShell outputs can be assigned to a variable. So, literally anything you do to $SomeVar that generates output even if the output is null can be assigned to $SomeOtherVar
Responding to Comment / Additional Example:
Copy$SomeVar = 'foo'
$SomeOtherVar = $SomeVar -replace 'foo', 'bar'
$SomeOtherVar
Output: bar
So I find this post somewhat confusing and amusing. Steven's answer is a good one but seems to not have gotten through somehow so I will try to just throw some code out there and hope something sticks. If not, at least I tried right?
Copyfunction Append-BarToString {
param(
[string]$InputString
)
# explicit return keyword not needed.
# Any output inside function not assigned
# to a variable or to $null will be sent out
$InputString + "Bar"
}
# function can also be written without param block
# more like C# like this, though it is unconventional
function Append-BarToString ([string]$InputString)
{
# $InputString + "Bar"
# or
"${InputString}Bar"
}
$someVar = 'foo'
$someOtherVar = Append-BarToString $someVar
$someOtherVar
# or
Write-Host $someOtherVar
# output
# fooBar
# fooBar
CMD does not have a straightforward way of assigning command output to a variable. If the command produces just a single line you could use a for loop
for /f "delims=" %a in ('some_command /to /run') do @set "var=%a"
However, if the command produces multiple lines that will capture only the last line. A better approach would be redirecting the output of the command to a file and then reading that file into a variable:
set "tempfile=C:\temp\out.txt"
>"%tempfile%" some_command /to /run
set /p var=<"%tempfile%"
del /q "%tempfile%"
If you literally need only the last file in a directory you don't need to run PowerShell, though. That much CMD can do by itself:
for /f "delims=" %f in ('dir /a-d /b') do @set "var=%~ff"
Beware that you need to double the % characters when running this from a batch file.
A FOR loop can provide the path to the file. If the default directory sorting order is not the result needed, specify additional command line switches on the DIR command.
FOR /F "delims=" %F IN ('DIR /B') DO (SET "THE_FILE=%~fF")
ECHO THE_FILE is "%THE_FILE%"
In a .bat file script, double the percent characters on FOR loop variables.
FOR /F "delims=" %%F IN ('DIR /B') DO (SET "THE_FILE=%%~fF")
ECHO THE_FILE is "%THE_FILE%"
The .bat file scripts can also run PowerShell scripts. It is best practice to not use aliases such as ls in scripts.
FOR /F "delims=" %%F IN ('powershell -NoLogo -NoProfile -Command ^
"(Get-ChildItem -File | Select-Object -Last 1).FullName"') DO (SET "THE_FILE=%%~fF")
ECHO THE_FILE is "%THE_FILE%"