Powershell Write-Host append to text file - computer name and time stamp

后端 未结 3 1505
执念已碎
执念已碎 2020-12-18 22:26

I am a Powershell noobie and I am currently writing my second script so bear with me. I am trying to do a write-host and output my write-host message along with

3条回答
  •  难免孤独
    2020-12-18 22:49

    Write-Host is only for sending data to the console and nowhere else. If you are looking to send data elsewhere ie. to file you will need to send it to the output stream

    write-output "folders created successfully $($env:computername)" >> c:\scripts\testlog.txt
    

    or

    "folders created successfully $($env:computername)" >> c:\scripts\testlog.txt
    

    Notice the sub expression around $env. We need to be sure that the variable is expanded in the double quotes properly.

    Look at Bacons answer for how to do this with native PowerShell cmdlets

提交回复
热议问题