I have written the code below, CreateLogFile
is a function and it will be called at the beginning of the script file.
Saved the file in desktop as TestPS.ps1
.
# Create a log file in the specified location.
CreateLogFile
#Create log file in the specified location and
#also change the name of existing log file.
Function CreateLogFile {
If ((Test-Path D:\PSLogs\PSLog.log)) {
$Date = Get-Date -UFormat '%Y%m%d%H%M%S'
$FileName = "PSLogs_" + $Date
#Write-Host $FileName
Get-ChildItem D:\PSLogs\PSLog.log |
Rename-Item -NewName {$_.name -replace '^PSLog', $FileName}
}
New-Item -Path D:\PSLogs\PSLog.log -ItemType File -Force
}
When I run the TestPS.ps1
file in PowerShell it is throwing the bellow error