none
Powershell Script to count the number of files in Temp folder and delete them RRS feed

  • Question

  • Hi Guys,

    Need your help to complete the script to delete temp files, but first i'm looking for print the count of files inside temp folder then delete them and again print the no of files which did not get deleted. The output will be stored in single file in csv format with column name like "no of files before", "Script status > Yes/No", "no of files after". Currently i'm able to print the count with Get-ChildItem & measure-object and delete them with Remove-item but need help in formatting the output in csv with Export-csv option. 

    Thanks in advance. :)

    • Moved by Bill_Stewart Tuesday, May 8, 2018 6:24 PM This is not "scripts on demand"
    Thursday, March 8, 2018 6:36 AM

All replies

  • Hi,

    There you go:

    $tempFiles = Get-ChildItem -Path C:\Windows\Temp -Recurse
    $tempFiles.Count
    $tempFiles | Remove-Item

    Thursday, March 8, 2018 7:30 AM
  • To format output to CSV you would put collect all the variables and then create a new psobject and then add all of the variables to this object:

    $I = New-Object PSObject
    
    $I | Add-Member NoteProperty CSVHEADERNAME1 $VARIABLENAME1
    $I | Add-Member NoteProperty CSVHEADERNAME2 $VARIABLENAME2
    
    $I | Export-CSV #PATHTOCSV -notypeinformation

    Hope this helps.

    Thursday, March 8, 2018 9:31 AM
  • I dont know whether i'm doing it right or wrong as i just started learning the scripting and need your help...currently its returning the output as "@{Count=67}" in cell, and where I want to only print the numerical value. I used below script,
    $VARIABLENAME1 = Get-ChildItem -Recurse -File -Path C:\Test | Measure-Object | select Count
    $VARIABLENAME2 = Get-ChildItem -Recurse -File -Path C:\HRM | Measure-Object | select Count
    
    
    $I = New-Object PSObject
    
    $I | Add-Member NoteProperty 'No of Files in Folder-1' $VARIABLENAME1
    $I | Add-Member NoteProperty 'No of Files in Folder-2' $VARIABLENAME2
    
    $I | Export-CSV -Path c:\99.csv -notypeinformation
    Please correct me where i'm doing wrong. 
    Friday, March 9, 2018 7:02 AM
  • 'C:\Test', 'C:\HRM' |
        ForEach-Object{
            [pscustomobject]@{    
                Folder = $_
                FileCount = (Get-ChildItem $_ -Recurse -File -Ea 0).Count
            }
            Remove-Item "$_\*" } | Export-CSV c:\99.csv -notypeinformation


    \_(ツ)_/




    • Edited by jrv Friday, March 9, 2018 7:30 AM
    Friday, March 9, 2018 7:25 AM