Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Deleting temp files on a remote server

$
0
0

Hi

I am very new to scripting world. I have written a basic code which does the job of deleting temp files under the listed folder path on a remote server. But as I see this process is taking quite a long time to process. I am hoping to find an other way to fasten this. Is it possible to push a script to the remote system first and run that locally? If I am not wrong that would make it process faster. 

I would also ask you to help me formatting this raw code. I want to introduce a logging system to this. Also make it work for multiple servers by fetching the list of servers from a csv. Thanks a lot in advance!!!!!

Code:

param (

    [string]$ComputerName = $(throw "ComputerName(s) is required.")

 

)

Clear-Host

 

 

    foreach ($computer in $ComputerName.Split(" "))

    {

        if (Test-Connection -ComputerName $Computer -ErrorAction SilentlyContinue)

        {

            Write-host ("About to clean : "+$computer)

 

$disk = ([wmi]"\\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")

"$Computer C: has {0:#.00} GB free of {1:#.00} GB Total - Before Cleaning" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output

 

Write-output "Cleaning is in progress, Please wait"

 

Remove-Item -Recurse -Force \\$ComputerName\c$\temp\*

Remove-Item -Recurse -Force \\$ComputerName\c$\'$Recycle.bin'\*

Remove-Item -Recurse -Force -erroraction 'silentlycontinue' \\$ComputerName\c$\Windows\Temp\*

 

$disk = ([wmi]"\\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")

"$Computer C: has {0:#.00} GB free of {1:#.00} GB Total - After Cleaning" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output

}

}

 

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles