I have the following error when running a script in powershell in version 5.1
Below my simple script to delete files over 180 days, I've tried some solutions but I haven't found what may be the error. (Below error translated from Portuguese to English)
"Out-File: It is not possible to validate the argument in the 'Encoding' parameter. The" files \ DeleteLogFile_21_01_2020.log "argument does not belong to the set" unknown; string; unicode; bigendianunicode; utf8; utf7; utf32; ascii; default; oem "specified by the ValidateSet attribute. Provide an argument that is in the set and try the command again. On the line: 1 character: 36 + $ Log | Out-File -Append D: \ Program files \ DeleteLogFile_ $ LogDate.log + ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo: InvalidData: (:) [Out-File], ParameterBindingValidationException + FullyQualifiedErrorId: ParameterArgumentValidationError, Microsoft .PowerShell.Commands.OutFileCommand "
$logPath = "D:\Program files\NICE Systems\Logs"
$nDays = 10
$startTime = Get-Date
$Extensions = "*.*"
$LogDate = (Get-Date).ToString("dd_MM_yyyy")
$Files = Get-Childitem $LogPath -Include $Extensions -Recurse | Where-Object {$_.LastWriteTime -le (Get-Date).AddDays(-$nDays)}
foreach ($File in $Files)
{
if ($NULL -ne $File )
{
$Log = "`nData e Hora da Deleção: " + $startTime + ". O arquivo " + $File + " foi deletado do sistema."
$Log | Out-File -Append D:\Program files\DeleteLogFile_$LogDate.log
Remove-Item $File.FullName | out-null
}
}
Out-File
is creating, and this answer may help. – Brandon McClure