VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

How To use variable in filename

Hi All,

filename giving error when used with multiple variable

$Today = Get-Date -format dd-MMM-yyyy-HH-mm-ss-tt

$serv = "myserver"

$filename = ".\"ExistingTranscript_ + $serv + _ + $Today".txt"

get-vm | out-file -path $filename

Error

At line:1 char:17

+ $filename = ".\"ExistingTranscript_ + $serv + _ + $Today".txt"

+                 ~~~~~~~~~~~~~~~~~~~

Unexpected token 'ExistingTranscript_' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : UnexpectedToken

Please help, how can I create a filename combining two or more variables

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Please try like this

$Today = Get-Date -format dd-MMM-yyyy-HH-mm-ss-tt

$serv = "myserver"


$filename = ".\ExistingTranscript_$serv$Today.txt"

Get-VM | Out-File -FilePath $filename


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Please try like this

$Today = Get-Date -format dd-MMM-yyyy-HH-mm-ss-tt

$serv = "myserver"


$filename = ".\ExistingTranscript_$serv$Today.txt"

Get-VM | Out-File -FilePath $filename


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much, it worked Smiley Happy

Reply
0 Kudos