none
Delete files/folders on remote servers using powershell

    Question

  • Hello,

     i am new to powershell.

    i am trying to delete some files & folders from remote computers (server-1, server-2. server-3, server-4) 

    Some are windows 2003 server are some are 2008

    folders to be deleted from server are been imported to script from files.txt (c:\temp\*.*, c:\Windows\temp\*.*, c:\recycler\*.*, c:\documents and settings\*.* -except Default, All users,Administrator. 60 (profiles older than 60 days)

    c:\windows\$NTUnunstaller , 30(older than 30 days), c:\users\*.* - except Default, All users, Administrator

    c:\$abcd$\*.* 30

    i am looking for script in powershell. which should support windows 2003 and win 2008 server

    Deleted file log should be saved to log.txt with date

    WinRM is Disabled in my network

    thanks in advance


    Thanks, jeevan


    Tuesday, June 25, 2013 2:19 AM

All replies

  • Are you saying that you already have a VBScript that does this job?  If so, please post the VBScript and a copy of files.txt , so we know exactly what input the script is getting.  It won't be any trouble to convert it to PowerShell, if that's what you're looking for.

    Tuesday, June 25, 2013 2:25 AM
  • thanks for quick reply David.

     i dont have that script in my system now iwill get it my tomorrow.

    and i am looking for Powershell script  which help me for 2008 as well

    ***** Deleting profiles in 2008 server except Default , All users, and  Administrator


    Thanks, jeevan

    Tuesday, June 25, 2013 2:37 AM
  • Hello David,

     sorry to day . i am not able to get that VBScript file, as that file is in private network

     i can give you the list of folders can be deleted, pls find the came below

    "C:\Temp\*"

    "C:\Logs\*"

    "C:\Documents adn settings\* 30 -exclude all users, Default User, Local Service, Administrator

    "C:\Recycler\*"

    "C:\Windows\Temp\*"

    "C:\Windows\$nt*" 30

    "C:\Windows\$hf_mig$\*" 30

    "C:\WinNT\$nt*" 30

    "C:\Winnt\$hf_mig$\*" 30

    this are the folder which i want to clear on remote servers


    Thanks, jeevan

    Wednesday, June 26, 2013 3:31 AM
  • I'd rather see the other script.  That way the PowerShell version will handle the same input file format.
    Wednesday, June 26, 2013 1:02 PM
  • Sorry David i am not able to get that script, as that script is in provate network,

     pls help me with Powershell script. 


    Thanks, jeevan

    Thursday, June 27, 2013 3:33 AM
  • Hi,

    Delete Files Remotely Using PowerShell Script

    http://adminpost.com/home/scripting/delete-files-remotely-using-powershell-script/

    http://www.networknet.nl/apps/wp/archives/943

    Hope it helps.

    Regards,

    Yan Li



    Cataleya Li
    TechNet Community Support

    Thursday, June 27, 2013 5:54 AM
  • Hi,

    I've written the script that realizes what you want. This script requires that WinRM is enabled on target systems. It uses a .csv file which contains rules to apply.

    The .csv File has the following Syntax :

    Columns détails :

    • Folder : specify the folder to clean
    • OlderThan : Specify the minimum age that element must have to be removed (value in days)
    • Exclusion : specify element name separated by commas that sould not be removed

    The script code is below :

    param (
        [string]$ComputerName = $(throw "ComputerName(s) is required."),
        [string]$FilePath = $(throw "CSV File path is required.")
    )
    Clear-Host
    
    if (Test-Path -Path ($FilePath))
    {
        $csv = Import-Csv -Path ($FilePath) -Delimiter ";"
    
        foreach ($computer in $ComputerName.Split(" "))
        {
            if (Test-Connection -ComputerName $Computer -ErrorAction SilentlyContinue)
            {
                Write-host ("About to clean : "+$computer)
                
                foreach ($item in $csv)
                {
                    if (Invoke-Command -ComputerName $Computer -ScriptBlock {param($element) Test-Path $element } -ArgumentList $item.Folder)
                    {
                        Write-Host ("`tAbout to process : `""+$item.Folder+"`"")
                        if ($item.Exclusion.Length -gt 0)
                        {
                            Write-Host ("`t`tApply exclusion List : "+$item.Exclusion) -ForegroundColor Yellow
                        }
                        if ($item.OlderThan.Length -gt 0)
                        {
                            Write-Host ("`t`tRemove items older than : "+$item.OlderThan+" days") -ForegroundColor Yellow
                        }
                
                        $command = ("Get-ChildItem -Path `""+$item.Folder+"`"")
                
                        if ($item.Exclusion.Length -gt 0)
                        {
                            $exclusionList = '@('
                            $item.Exclusion.Split(",") | ForEach-Object{$_.Trim()} | ForEach-Object{$exclusionList += ("`""+$_+"`""+",")}
                            $exclusionList = $exclusionList.Substring(0, $exclusionList.Length-1)
                            $exclusionList += ')'
                            $command += (' | Where-Object{$_.Name -notin '+ $exclusionList +"}")
                    
                        }
    
                        try
                        {
                            $Error.Clear()
                            $elements = Invoke-Command -ComputerName $Computer -ScriptBlock {param($cmd) Invoke-Expression $cmd} -ArgumentList $command
                        }
                        Catch [System.UnauthorizedAccessException]
                        {
                            Write-Host ("`tError : "+($Error[0].exception).Message) -ForegroundColor Red
                        }
                
                
                        if ($item.OlderThan.Length -gt 0) { $elements = $elements | Where-Object{$_.LastWriteTime -lt ([System.dateTime]::Now).AddDays(-$item.OlderThan)}}
                        
                        if ($elements -ne $null){Invoke-Command -ComputerName $Computer -ScriptBlock {param($itemsToRemove) $itemsToRemove | ForEach-Object{Write-Host ("`t`tRemoving : `"" + $_+"`" - ") -NoNewline; Remove-Item -Path $_ -Force -Recurse; Write-Host ("Success") -ForegroundColor Green }} -ArgumentList (,($elements).FullName)}
                        
    
                        Invoke-Command -ComputerName $Computer -ScriptBlock {param($itemToRemove) if((Get-ChildItem -path $itemToRemove).Count -eq 0){Remove-Item -Path $itemToRemove -Recurse -Force }} -ArgumentList (,$item.Folder)
    
                        $command = $null
                        $exclusion = $null
                        $exclusionList = $null
                        $elements = $null
                    }
                    else {Write-Host ("Skip : `""+$item.Folder+"`"") -ForegroundColor Gray}
                }
            }
            else
            {
                Write-host ("Unable to connect to : "+$computer) -ForegroundColor Red
            }
            Write-Host ""
        }    
    }
    else
    {
        Exit 1
    }
    

    You must specify 2 arguments to run this script : computername and filePath. For example :

    .\RemoveFilesAndFolders.ps1 -ComputerName SC-SCCM,PORT-RLA -FilePath .\List.csv

    The following screenshot present you the output :

    Regards,

    Régis

    Thursday, June 27, 2013 11:47 AM
  • Hello Laine,

    WinRM is Disabled in our network on target systems.

    do you have any other process to do that Without WinRM


    Thanks, jeevan

    Monday, July 01, 2013 5:24 PM