PowerShell: concatenate strings with variables after cmdlet -


i new powershell. have looked online answer, no avail. perhaps i'm phrasing question incorrectly.

i find myself in situation have concatenate string variable after cmdlet. example,

new-item $archive_path + "logfile.txt" -type file 

if try run this, powershell throws following error:

new-item : positional parameter cannot found accepts argument '+'.

am not concatenating string correctly? i'd not have declare variable before each cmdlet in (e.g., $logfile = $archive_path + "logfile.txt", new-item $logfile -type file). also, won't concatenating file path.

you error because powershell parser sees $archive_path, +, , "logfile.txt" 3 separate parameter arguments, instead of 1 string.

enclose string concatenation in parantheses () change order of evaluation:

new-item ($archive_path + "logfile.txt") -type file 

or enclose variable in subexpression:

new-item "$($archive_path)logfile.txt" -type file 

you can read argument mode parsing get-help about_parsing


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -