copy-item where file name is the computer hostname

前端 未结 3 1840
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 17:32

Having what seems to be a basic problem in Powershell. I am doing a simple copy-item script from a UNC share to the local C:\\ drive. Here is my code:



        
相关标签:
3条回答
  • 2021-01-14 18:02

    You could also directly embed the environment variable in double quoted string:

    Copy-Item "\\server\share\$env:computername.txt" C:\
    
    0 讨论(0)
  • 2021-01-14 18:08

    You need to replace your single quotes with double quotes like this:

    $hostname = $env:computername
    
    Copy-Item -Path "\\server\share\$hostname.txt" -Destination 'C:\'
    
    0 讨论(0)
  • 2021-01-14 18:13

    Single quoted strings will not expand variables. You need a double quoted string e.g.:

    Copy-Item "\\server\share\$hostname.txt" C:\
    

    And in general, you don't need to quote string arguments to a PowerShell command unless you need variable expansion or there's a space in the string.

    0 讨论(0)
提交回复
热议问题