Link to home
Start Free TrialLog in
Avatar of footech
footechFlag for United States of America

asked on

PowerShell syntax

So I recently read a post that used some syntax I'm unfamiliar with (I've bolded the relevant portion).
"C:\temp\logs\${env:computername}_$(get-date -f dd-MM-yyyy)_CHKDSKResults.txt"

I'm certainly familiar with the use of subexpressions, $(), but the portion in question uses braces instead of parentheses.  I've never seen that syntax before and have not seen any mention of it, yet it works.  If someone has asked me to come up with the equivalent, I would have used the following:
"C:\temp\logs\$($env:computername)_$(get-date -f dd-MM-yyyy)_CHKDSKResults.txt"

So here's the actual question.  Can anyone point me toward documentation or a resource that describes this syntax, and possibly what can be done with it?
And in the sample given, I'm not even sure how ${env:computername} works.  It's like running (gi env:\computername).value

My poor soul craves enlightenment!
SOLUTION
Avatar of Britt Thompson
Britt Thompson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of footech

ASKER

@renazonse - Your comment wasn't quite clear, but it did lead me to playing around with variable names, and then re-reading the about_Variables topic (built-in PS help) which actually mentions it.  Do you have any more information on its use to call functions "via the invoke commands"?  I'm not sure what you mean by "invoke commands", unless you just mean the cmdlet Invoke-Command, but I'm not seeing the connection.  Example(s) would be really helpful, please!

@oBdA - I was hoping you would spot that.  Thanks for the link!  
I had a question about working with drives after reading reading renazonse's mention about functions, and seeing your example with env:, so I'm glad to see the mention in the reading about using this notation to work with PS drives.
function Processes ($Item) { Get-Process | ?{$_.Name -eq $Item} }
$Item = "spoolsv"
$S = New-PSSession -ComputerName $Computer -Credential $WindowsPass
$Data = Invoke-Command -Session $S -ScriptBlock ${function:Processes} -ArgumentList $Item
Avatar of footech

ASKER

It took some further investigation, but I finally figured out how that works.
Running ${function:Processes} just returns the scriptblock defining the function.  Same as if you had run
gi function:\Processes | Select -expand Scriptblock
Avatar of footech

ASKER

Thank you both.