Global Files-Delete Function

Ever wanted to just pass the name of your file to a function and have that function find the file(s) in question regardless on what partition, what drive or what folder/subfolder they reside in?

Well, here’s one such function, its split into 2 parts. I prefix my module with the filename after the -. Hence why this would translate to Files-Get and Files-Global-Erase. If you want to target multiple files with the same name but different extension, then simply enter the filename only, no extension; if you wish to erase 1 specific file, then enter name and extension for that kind of accuracy:

function Get() {

    <#

        .SYNOPSIS

        Get items(s) from specified path.

        .DESCRIPTION

        Uses Get-ChildItem cmdlet to procure children of folder of file.

        .INPUTS

        No inputs.

        .OUTPUTS

        Get-ChildItem object or collection.

        .EXAMPLE

        Get -Item "c:\Item" -Types "*.txt", "*.log"

        .EXAMPLE

        Files-Get -Item "c:\Item" -Types "*.txt", "*.log"


    #>

    Param (
        [string]$Item,
        [string[]]$Types=$null
    )

    If (!$Item) {

        $Item = Read-Host "Enter item path, e.g. c:\MyDirectory or c:\MyDirectory\MyFile.file"

    }

    if (!$Types) {

        $Types = Read-Host "File types array to target, e.g. *.txt, *.pdf"

    }   

    $ErrorActionPreference = "silentlycontinue"
    Get-ChildItem $Item -Include $Types -Recurse

}

function Global-Erase() {

    <#

        .SYNOPSIS

        Delete files from any location or multiple locations. 

        .DESCRIPTION

        Uses Get-Partition and Get-ChildItem cmdlets to find file, and delete. 

        .INPUTS

        File object

        .OUTPUTS

        No outputs.

        .EXAMPLE

        Global-Erase -fileToDelete $file

        .EXAMPLE

        Global-Files-Erase -fileToDelete $file


    #>

    Param (
        $fileToDelete
    )
    
    #Get all HDD partitions
    $partitions =  Get-Partition;

    if ($partitions) {
        #if there are partitions
        #get only accessible partitions with drive letter
        $drives = $partitions.DriveLetter | Where-Object {$_ -ne 0x00}

        #loop through list
        foreach ($drive in $drives) {
            $files = Files-Get ($drive+":\") "*$($fileToDelete.Substring($fileToDelete.length-4, 4))" | Where-Object {$_.Name -match $fileToDelete}
            $paths = foreach ($file in $files) {"$($file.Directory)\$($file.Name)"}
            Files-Delete $paths
        }
    }
    else {
        #just compute c drive
        $files = Files-Get c:\ "*$($fileToDelete.Substring($fileToDelete.length-4, 4))" | Where-Object {$_.Name -match $fileToDelete}
        $paths = foreach ($file in $files) {"$($file.Directory)\$($file.Name)"}
        Files-Delete $paths

    }


}
0
Resource management in your PowerShell framework Capture objects and export them using PowerShell

No Comments

No comments yet

Leave a Reply

Your email address will not be published. Required fields are marked *