Use Move-Item in PowerShell to append Hostname and date to file name

Viewed 754

Complete PowerShell newbie so please forgive me as I might mess up what I am asking.

I have a file called debug.log, I need to rename it and move it.

I know I can do that with Move-Item, but I would like to append the Hostname to the front of the file name and the date stamp to the end.

For example: In: C:\Temp\debug.log Out: \\\sharename\MyHostdebug20201006.log

The files will go through a log parse after they get copied to the share, and the HostName and Date stamp are import to my tracking of which logs get processed. I have to do this for 8 separate servers on a monthly basis. I plan on setting this up as a Windows Schedule Task when it is completed.

1 Answers

Using Get-Date and, as mentioned by @Scepticalist above, $env:ComputerName you can create a string variable which you can pass into your Move-Item as the destination:

$timestamp = get-date -Format yyyyMMdd
$newPath = "\\sharename\" + $env:computername + "debug" + $timestamp + ".log"
Related