I am new to windows powershell. I have a script to identify the files and folders older than x files and delete it. but I need a script to check the same in remote windows server and do the same. Here is my code:
$Now = Get-Date
$Days = "3"
$TargetFolder = "D:\hudson_slave\workspace"
$LastWrite = $Now.AddDays(-$Days)
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{
if ($File -ne $NULL)
{
write-host "Deleting File $File" -ForegroundColor "DarkRed"
Remove-Item $File.FullName | out-null
}
else
{
Write-Host "No more files to delete!" -foregroundcolor "Green"
}
}
Could any one please help me to delete the 3 days older files and folders in remote server (using IP)
As Oggew stated you can delete them through a share
$TargetFolder = "\\server\sharename\folder"
You can also create and enter a PSSession. This is like you would work in a powershell console on the remote host.
Enter-PSSession servername
This way you can run your code as is. To use this you must enable PSRemoting first. Please take a look at about_remote .
Additionally you can use this command to remove the files.
$Files | Remove-Item
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.