I'm using a text file with a list of file and folder paths that I'd like to copy to some $destination. I'm trying to get powershell to interpret each line as a file path for copy-item. Here's what I'm working with:
$cfglist = Get-Content "${cfgPath}\ProgramConfigsList.txt"
Foreach($item in $cfglist){
#Copy-Item -Path $item -Destination $dst -Force -Recurse -Container:$true -Passthru -Whatif
}
ProgramConfigsList.txt looks like this:
"$env:programfiles\Tracker Software\PDF Editor\Dictionaries"
"${env:ProgramFiles(x86)}\Notepad++\plugins"
"$env:userprofile\AppData\Local\ATI"
and so on.
I get this error: Copy-Item : Cannot find drive. A drive with the name '"$env' does not exist.
Within the text file, I've tried using quotes, not using quotes, and de-commenting ":" with `. Within the PS script, I've $item, "$item", "${item}", and ${item}. I've also tried piping the $cfglist variable to a foreach-object script block with the same copy-item command. I've tried using variations of 'get-childitem', but that doesn't seem to work with entries inside a text file that need to be interpreted as file/folder paths. I also created an array from the entries within ProgramConfigsList.txt inside the ps1 script, but that ran into the same error.
Any suggestions?
Invoke-Expression
for string expansion alone. What if someone puts something nasty in the file your reading? :-| – Mathias R. Jessen%environmentvariablename%
syntax in your input file; then you can simply use[Environment]::ExpandEnvironmentVariables
in PowerShell and be done with it. – Bill_Stewart