Guide to Using PowerShell for File Extension Reports in IT Management

In today’s digital landscape, managing files efficiently is crucial for IT professionals and Managed Service Providers (MSPs). The ability to generate comprehensive reports based on file extensions within specified directories can streamline operations, enhance security, and aid in maintaining system hygiene. This post explores a PowerShell script designed to automate this task, ensuring IT professionals can effortlessly locate and document files by extension across various directories.

Background 

PowerShell scripts are invaluable tools for IT administrators, providing the automation necessary to handle complex tasks with precision and ease. This particular script focuses on generating detailed reports based on file extensions within user-specified directories and their subdirectories. By leveraging this script, IT professionals can quickly identify specific file types, facilitating tasks such as software audits, compliance checks, and security assessments.

The Script

#Requires -Version 5.1

<#
.SYNOPSIS
    Creates a report based on the files found in the directory or subdirectory you specified with your desired extension.
.DESCRIPTION
    Creates a report based on the files found in the directory or subdirectory you specified with your desired extension.
.EXAMPLE
    -Extensions ".exe" -SearchPaths "C:\Users\tuser\Downloads"
    
    Searching C:\Users\tuser\Downloads for files with extension '.exe'...
    No files found with extension .exe!


PARAMETER: -Extensions "exe, .ico"
    A comma-separated list of extensions to search for. You can use the * character as a wildcard.

PARAMETER: -SearchPaths "C:\Replace\Me\With\Valid\Path"
    Enter the starting directories for the search, separated by commas. This will include all subdirectories as well.

PARAMETER: -MultiLineField "ReplaceMeWithNameOfMultilineCustomField"
    Optional multiline field to record search results. Leave blank if unused.

PARAMETER: -WysiwygField "ReplaceMeWithNameOfWYSIWYGCustomField"
    Optional WYSIWYG field to record search results. Leave blank if unused.

PARAMETER: -ScanSystemDrive
    This will set the system drive (usually drive C:\) as the starting point for the search.

PARAMETER: -ScanAllDrives
    This will set all drives (including flash drives) as the starting point for the search.
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes: Initial Release
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>

[CmdletBinding()]
param (
    [Parameter()]
    [String]$Extensions,
    [Parameter()]
    [String]$SearchPaths,
    [Parameter()]
    [String]$MultiLineField,
    [Parameter()]
    [String]$WysiwygField,
    [Parameter()]
    [Switch]$ScanSystemDrive = [System.Convert]::ToBoolean($env:scanSystemDrive),
    [Parameter()]
    [Switch]$ScanAllDrives = [System.Convert]::ToBoolean($env:scanAllDrives)
)
begin {
    # Set parameters using dynamic script variables.
    if ($env:fileExtension -and $env:fileExtension -notlike "null") { $Extensions = $env:fileExtension }
    if ($env:searchPath -and $env:searchPath -notlike "null") { $SearchPaths = $env:searchPath }
    if ($env:multilineCustomField -and $env:multilineCustomField -notlike "null") { $MultiLineField = $env:multilineCustomField }
    if ($env:wysiwygCustomField -and $env:wysiwygCustomField -notlike "null") { $WysiwygField = $env:wysiwygCustomField }

    # Check if no extensions were specified and exit with an error if true.
    if (-not $Extensions) {
        Write-Host -Object "[Error] Missing extension to search for!"
        exit 1
    }

    # Verify that WysiwygField and MultiLineField are not the same, exiting with an error if they are.
    if ($WysiwygField -and $MultiLineField -and ($WysiwygField -eq $MultiLineField)) {
        Write-Host -Object "[Error] Wysiwyg Field and Multiline Field are the same! Custom fields cannot be the same type."
        Write-Host -Object "https://ninjarmm.zendesk.com/hc/en-us/articles/18601842971789-Custom-Fields-by-Type-and-Functionality"
        exit 1
    }

    # Initialize a list to store the extensions to search for.
    $ExtensionsToSearch = New-Object System.Collections.Generic.List[string]
    # Split the extensions if they are comma-separated and trim whitespace.
    if ($Extensions -match ",") {
        $Extensions -split "," | ForEach-Object { $ExtensionsToSearch.Add($_.Trim()) }
    }
    else {
        $ExtensionsToSearch.Add($Extensions.Trim())
    }
    
    # Initialize a list to keep track of extensions that need to be replaced (adding a leading dot if missing).
    $ExtensionsToReplace = New-Object System.Collections.Generic.List[object]
    $ExtensionsToSearch | ForEach-Object {
        if ($_ -notmatch "^\.") {
            $NewExtension = ".$_"

            $ExtensionsToReplace.Add(
                [PSCustomObject]@{
                    Index        = $ExtensionsToSearch.IndexOf("$_")
                    NewExtension = $NewExtension
                }
            )
                
            Write-Warning "Missing . for extension. Changing extension search to '$NewExtension'."
        }
    }

    # Apply the replacements for extensions that were missing a leading dot.
    $ExtensionsToReplace | ForEach-Object {
        $ExtensionsToSearch[$_.index] = $_.NewExtension 
    }

    # Check if no search locations were specified and exit with an error if true.
    if (!$SearchPaths -and !$ScanSystemDrive -and !$ScanAllDrives) {
        Write-Host -Object "[Error] Missing somewhere to search!"
        exit 1
    }

    # If scanning all drives, ignore specific paths and the system drive flag.
    if ($ScanAllDrives) {
        $ScanSystemDrive = $false
        $SearchPaths = $Null
    }

    # Initialize a list for paths to search.
    $PathsToSearch = New-Object System.Collections.Generic.List[string]
    # Split the search paths if they are comma-separated and trim whitespace.
    if ($SearchPaths -match ",") {
        $SearchPaths -split "," | ForEach-Object { $PathsToSearch.Add($_.Trim()) }
    }
    elseif ($SearchPaths) {
        $PathsToSearch.Add($SearchPaths)
    }

    # Add the system drive to the search paths if specified.
    if ($ScanSystemDrive) {
        if ($env:SystemDrive -notmatch '^[A-Z]:\\$' -and $env:SystemDrive -match '^[A-Z]:$') {
            $PathsToSearch.Add("$env:SystemDrive\")
        }
        else {
            $PathsToSearch.Add($env:SystemDrive)
        }
    }

    # Add all filesystem drives to the search paths if scanning all drives.
    if ($ScanAllDrives) {
        Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Free -and $_.Used } | ForEach-Object {
            if ($_.Root -notmatch '^[A-Z]:\\$' -and $_.Root -match '^[A-Z]:$') {
                $PathsToSearch.Add("$($_.Root)\")
            }
            else {
                $PathsToSearch.Add($_.Root)
            }
        }
    }

    # Initialize a list for paths that need to be corrected (adding a trailing backslash if missing).
    $ReplacementPaths = New-Object System.Collections.Generic.List[Object]

    # Check each path and add a backslash if it's missing.
    $PathsToSearch | ForEach-Object {
        if ($_ -notmatch '^[A-Z]:\\$' -and $_ -match '^[A-Z]:$') {
            $NewPath = "$_\"
            $ReplacementPaths.Add(
                [PSCustomObject]@{
                    Index   = $PathsToSearch.IndexOf("$_")
                    NewPath = $NewPath
                }
            )
                
            Write-Warning "Backslash missing from the search path. Changing it to $NewPath."
        }
    }

    # Apply the path corrections.
    $ReplacementPaths | ForEach-Object {
        $PathsToSearch[$_.index] = $_.NewPath 
    }

    # Function to test if the script is running with elevated permissions.
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }

    # Handy function to set a custom field.
    function Set-NinjaProperty {
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory = $True)]
            [String]$Name,
            [Parameter()]
            [String]$Type,
            [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
            $Value,
            [Parameter()]
            [String]$DocumentName
        )

        $Characters = $Value | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters
        if ($Characters -ge 200000) {
            throw [System.ArgumentOutOfRangeException]::New("Character limit exceeded, value is greater than or equal to 200,000 characters.")
        }
    
        # If we're requested to set the field value for a Ninja document we'll specify it here.
        $DocumentationParams = @{}
        if ($DocumentName) { $DocumentationParams["DocumentName"] = $DocumentName }
    
        # This is a list of valid fields that can be set. If no type is given, it will be assumed that the input doesn't need to be changed.
        $ValidFields = "Attachment", "Checkbox", "Date", "Date or Date Time", "Decimal", "Dropdown", "Email", "Integer", "IP Address", "MultiLine", "MultiSelect", "Phone", "Secure", "Text", "Time", "URL", "WYSIWYG"
        if ($Type -and $ValidFields -notcontains $Type) { Write-Warning "$Type is an invalid type! Please check here for valid types. https://ninjarmm.zendesk.com/hc/en-us/articles/16973443979789-Command-Line-Interface-CLI-Supported-Fields-and-Functionality" }
    
        # The field below requires additional information to be set
        $NeedsOptions = "Dropdown"
        if ($DocumentName) {
            if ($NeedsOptions -contains $Type) {
                # We'll redirect the error output to the success stream to make it easier to error out if nothing was found or something else went wrong.
                $NinjaPropertyOptions = Ninja-Property-Docs-Options -AttributeName $Name @DocumentationParams 2>&1
            }
        }
        else {
            if ($NeedsOptions -contains $Type) {
                $NinjaPropertyOptions = Ninja-Property-Options -Name $Name 2>&1
            }
        }
    
        # If an error is received it will have an exception property, the function will exit with that error information.
        if ($NinjaPropertyOptions.Exception) { throw $NinjaPropertyOptions }
    
        # The below type's require values not typically given in order to be set. The below code will convert whatever we're given into a format ninjarmm-cli supports.
        switch ($Type) {
            "Checkbox" {
                # While it's highly likely we were given a value like "True" or a boolean datatype it's better to be safe than sorry.
                $NinjaValue = [System.Convert]::ToBoolean($Value)
            }
            "Date or Date Time" {
                # Ninjarmm-cli expects the GUID of the option to be selected. Therefore, the given value will be matched with a GUID.
                $Date = (Get-Date $Value).ToUniversalTime()
                $TimeSpan = New-TimeSpan (Get-Date "1970-01-01 00:00:00") $Date
                $NinjaValue = $TimeSpan.TotalSeconds
            }
            "Dropdown" {
                # Ninjarmm-cli is expecting the guid of the option we're trying to select. So we'll match up the value we were given with a guid.
                $Options = $NinjaPropertyOptions -replace '=', ',' | ConvertFrom-Csv -Header "GUID", "Name"
                $Selection = $Options | Where-Object { $_.Name -eq $Value } | Select-Object -ExpandProperty GUID
    
                if (-not $Selection) {
                    throw [System.ArgumentOutOfRangeException]::New("Value is not present in dropdown")
                }
    
                $NinjaValue = $Selection
            }
            default {
                # All the other types shouldn't require additional work on the input.
                $NinjaValue = $Value
            }
        }
    
        # We'll need to set the field differently depending on if its a field in a Ninja Document or not.
        if ($DocumentName) {
            $CustomField = Ninja-Property-Docs-Set -AttributeName $Name -AttributeValue $NinjaValue @DocumentationParams 2>&1
        }
        else {
            $CustomField = $NinjaValue | Ninja-Property-Set-Piped -Name $Name 2>&1
        }
    
        if ($CustomField.Exception) {
            throw $CustomField
        }
    }

    $ExitCode = 0
}
process {
    # Check if the script is running with Administrator privileges. Exit with an error message if not.
    if (!(Test-IsElevated)) {
        Write-Host -Object "[Error] Access Denied. Please run with Administrator privileges."
        exit 1
    }

    # Remove illegal extensions
    $ExtensionsToRemove = New-Object System.Collections.Generic.List[String]
    $invalidExtensions = '[<>:"/\\|\x00-\x1F]|\.$'
    $ExtensionsToSearch | ForEach-Object {
        if($_ -match $invalidExtensions){
            Write-Host -Object "[Error] Extension $_ contains one of the following invalid characters or ends in a period. '\:<>`"/|'"
            $ExtensionsToRemove.Add($_)
            $ExitCode = 1
        }
    }

    # Actual removal
    $ExtensionsToRemove | ForEach-Object {
        $ExtensionsToSearch.Remove($_) | Out-Null
    }

    # Exit the script if there are no valid extensions left to search.
    if ($ExtensionsToSearch.Count -eq 0) {
        Write-Host "[Error] No valid extensions to search!"
        exit 1
    }

    # Initialize lists to store information about paths and errors.
    $CustomFieldErrorInfo = New-Object System.Collections.Generic.List[string]

    # These characters are not valid for a search path.
    $invalidSearchPathCharacters = '[<>"/|?\x00-\x1F]'

    # Initialize a generic list to store paths that don't exist and should be removed from the search.
    $PathsToRemove = New-Object System.Collections.Generic.List[String]
    # Check each path in the search list to ensure it exists. Collect paths that don't exist for removal.
    $PathsToSearch | ForEach-Object {
        if($_ -match $invalidSearchPathCharacters){
            Write-Host -Object "[Error] Path $_ contains one of the following invalid characters. '<>`"/|'"
            $PathsToRemove.Add($_)
            $ExitCode = 1
            return
        }

        if (!(Test-Path $_)) {
            Write-Host -Object "[Error] $_ does not exist!"
            $PathsToRemove.Add($_)
            $ExitCode = 1
        }
    }

    # Remove non-existing paths from the search list.
    $PathsToRemove | ForEach-Object {
        $PathsToSearch.Remove($_) | Out-Null
    }

    # Exit the script if there are no valid paths left to search.
    if ($PathsToSearch.Count -eq 0) {
        Write-Host "[Error] No valid paths to search!"
        exit 1
    }

    # Initialize a list to keep track of the search jobs created.
    $SearchJobs = New-Object System.Collections.Generic.List[object]

    # Create and start a PowerShell job for each path and extension combination.
    foreach ($Path in $PathsToSearch) {
        foreach ($Extension in $ExtensionsToSearch) {
            Write-Host "Searching '$Path' for files with extension '$Extension'..."
            $SearchJobs.Add(
                (
                    Start-Job -ScriptBlock {
                        param($Path, $Extension)

                        # Defines a function to convert file sizes to a human-readable format.
                        function Get-FriendlySize {
                            param($Bytes)
                            # Converts Bytes to the highest matching unit
                            $Sizes = 'Bytes,KB,MB,GB,TB,PB,EB,ZB' -split ','
                            for ($i = 0; ($Bytes -ge 1kb) -and ($i -lt $Sizes.Count); $i++) { $Bytes /= 1kb }
                            $N = 2
                            if ($i -eq 0) { $N = 0 }
                            if ($Bytes) { "$([System.Math]::Round($Bytes,$N)) $($Sizes[$i])" }else { "0 B" }
                        }

                        # Search for files matching the extension and output their details in CSV format.
                        Get-ChildItem -Path $Path -Filter "*$Extension" -Recurse -File -Force | Select-Object Name, FullName, CreationTime, LastWriteTime, Length, @{Name = "Size"; Expression = { Get-FriendlySize $_.Length } } | ConvertTo-Csv
                    } -ArgumentList $Path, $Extension
                )
            )
        }
    }

    # Wait for all search jobs to complete or timeout after 9000 seconds (2.5 hours).
    $SearchJobs | Wait-Job -Timeout 9000 | Out-Null

    # Check for incomplete jobs due to timeout and log an error.
    $IncompleteJobs = $SearchJobs | Get-Job | Where-Object { $_.State -eq "Running" }
    if ($IncompleteJobs) {
        Write-Host "[Error] The timeout period of 2.5 hours has been reached, but not all files or directories have been searched!"
        $CustomFieldErrorInfo.Add("[Error] The timeout period of 2.5 hours has been reached, but not all files or directories have been searched!")
        $ExitCode = 1
    }

    # Collect and process the output from each search job.
    $MatchingItems = $SearchJobs | ForEach-Object {
        $_ | Get-Job | Receive-Job -ErrorAction SilentlyContinue -ErrorVariable JobErrors | ConvertFrom-Csv
    }

    # Clear out duplicate entries
    if ($MatchingItems) {
        $MatchingItems = $MatchingItems | Sort-Object FullName -Unique
    }

    # Check for jobs that failed to complete successfully and log errors.
    $FailedJobs = $SearchJobs | Get-Job | Where-Object { $_.State -ne "Completed" }
    if ($JobErrors -or $FailedJobs) {
        $CustomFieldErrorInfo.Add("[Error] Failed to search certain directories due to an error.")

        if ($JobErrors) {
            $JobErrors | ForEach-Object { 
                $CustomFieldErrorInfo.Add("[Error] $($_.Exception.Message)")
            }
        }
        $ExitCode = 1
    }

    # Process and attempt to set custom field values based on search results and errors, with specific handling for multiline fields.
    # Truncate data if it exceeds character limits for the fields.
    if ($MultiLineField -and $MatchingItems) {
        try {
            Write-Host "Attempting to set Custom Field '$MultiLineField'."

            # Prepare the custom field output.
            $CustomFieldValue = New-Object System.Collections.Generic.List[string]

            # We don't want to edit the matching items array if we have to truncate later so we'll create a duplicate here.
            $CustomFieldList = $MatchingItems | Select-Object -Property Name, FullName, CreationTime, LastWriteTime, Size

            # Format the matching items into a nice list with the relevant properties.
            $CustomFieldValue.Add(($CustomFieldList | Format-List -Property Name, FullName, CreationTime, LastWriteTime, Size | Out-String))
            
            # If any errors were encountered in the search add them to the bottom of the custom field output.
            $CustomFieldErrorInfo | ForEach-Object {
                $CustomFieldValue.Add($_)
            }
            
            # Check that the output complies with the hard character limits.
            $Characters = $CustomFieldValue | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters
            if ($Characters -ge 9500) {
                Write-Warning "10,000 Character Limit has been reached! Trimming output until the character limit is satisified..."
                
                # If it doesn't comply with the limits we'll need to recreate it with some adjustments.
                $i = 0
                do {
                    # Recreate the custom field output starting with a warning that we truncated the output.
                    $CustomFieldValue = New-Object System.Collections.Generic.List[string]
                    $CustomFieldValue.Add("This info has been truncated to accommodate the 10,000 character limit.")
                    
                    # The custom field information is sorted in alphabetical order. We'll flip the array upside down to sort it in reverse alphabetical.
                    [array]::Reverse($CustomFieldList)

                    # Remove the next item which in this case will be the smallest item.
                    $CustomFieldList[$i] = $null
                    $i++

                    # We'll flip the array back to right side up.
                    [array]::Reverse($CustomFieldList)

                    # Add it back to the output.
                    $CustomFieldValue.Add(($CustomFieldList | Format-List -Property Name, FullName, CreationTime, LastWriteTime, Size | Out-String))
                    # Finish with adding any errors we encountered during the search.
                    $CustomFieldErrorInfo | ForEach-Object {
                        $CustomFieldValue.Add($_)
                    }

                    # Check that we now comply with the character limit. If not restart the do loop.
                    $Characters = $CustomFieldValue | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters
                }while ($Characters -ge 9500)
            }

            # Set the custom field.
            Set-NinjaProperty -Name $MultiLineField -Value $CustomFieldValue
            Write-Host "Successfully set Custom Field '$MultiLineField'!"
        }
        catch {
            Write-Host "[Error] $($_.Exception.Message)"
            $ExitCode = 1
        }
    }

    # Process and attempt to set custom field values based on search results and errors, with specific handling for WYSIWYG fields.
    # Truncate data if it exceeds character limits for the fields.
    if ($WysiwygField -and $MatchingItems) {
        try {
            Write-Host "Attempting to set Custom Field '$WysiwygField'."

            # Prepare the custom field output.
            $CustomFieldValue = New-Object System.Collections.Generic.List[string]

            # Convert the matching items into an html report.
            $htmlTable = $MatchingItems | Select-Object -Property Name, FullName, CreationTime, LastWriteTime, Size | ConvertTo-Html -Fragment
            
            # Add the newly created html into the custom field output.
            $CustomFieldValue.Add($htmlTable)
            # If any errors were encountered in the search add them to the bottom of the custom field output.
            $CustomFieldErrorInfo | ForEach-Object {
                $CustomFieldValue.Add($_)
            }

            # Check that the output complies with the hard character limits.
            $Characters = $CustomFieldValue | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters
            if ($Characters -ge 199500) {
                Write-Warning "200,000 Character Limit has been reached! Trimming output until the character limit is satisified..."
                
                # If it doesn't comply with the limits we'll need to recreate it with some adjustments.
                $i = 0
                do {
                    # Recreate the custom field output starting with a warning that we truncated the output.
                    $CustomFieldValue = New-Object System.Collections.Generic.List[string]
                    $CustomFieldValue.Add("<h1>This info has been truncated to accommodate the 200,000 character limit.</h1>")

                    # The custom field information is sorted in alphabetical order. We'll sort it into reverse alphabetical by flipping the array upside down.
                    [array]::Reverse($htmlTable)
                    # If the next entry is a row we'll delete it.
                    if ($htmlTable[$i] -match '<tr><td>') {
                        $htmlTable[$i] = $null
                    }
                    $i++
                    # We'll flip the array back to right side up.
                    [array]::Reverse($htmlTable)

                    # Add it back to the output.
                    $CustomFieldValue.Add($htmlTable)
                    # Finish with adding any errors we encountered during the search.
                    $CustomFieldErrorInfo | ForEach-Object {
                        $CustomFieldValue.Add($_)
                    }
                    # Check that we now comply with the character limit. If not restart the do loop.
                    $Characters = $CustomFieldValue | Out-String | Measure-Object -Character | Select-Object -ExpandProperty Characters
                }while ($Characters -ge 199500)
            }

            # Set the custom field.
            Set-NinjaProperty -Name $WysiwygField -Value $CustomFieldValue
            Write-Host "Successfully set Custom Field '$WysiwygField'!"
        }
        catch {
            Write-Host "[Error] $($_.Exception.Message)"
            $ExitCode = 1
        }
    }

    # Output the results of our search into the activity log.
    if (!$MatchingItems) {
        Write-Host "No files found with extension $Extension!"
    }
    else {
        Write-Host "Files found!"
        $MatchingItems | Format-List -Property Name, FullName, CreationTime, LastWriteTime, Size | Out-String | Write-Host
    }

    # If we encountered any errors during the search we'll output them here.
    if ($JobErrors -or $FailedJobs) {
        Write-Host ""
        Write-Host "[Error] Failed to search certain directories due to an error."

        if ($JobErrors) {
            Write-Host ""

            $JobErrors | ForEach-Object {
                Write-Host "[Error] $($_.Exception.Message)" 
            }
        }
        $ExitCode = 1
    }

    # Remove all jobs to clean up.
    $SearchJobs | Get-Job | Remove-Job -Force

    # Exit the script with the appropriate exit code
    exit $ExitCode
}
end {
    
    
     
}

 

Access over 300+ scripts in the NinjaOne Dojo

Get Access

Detailed Breakdown

Script Synopsis

The script is designed to search directories for files with specific extensions and create a comprehensive report. It accepts various parameters, including extensions to search for, directories to search within, and optional custom fields for recording results.

Parameters

  1. Extensions: A comma-separated list of file extensions to search for. Wildcards are supported.
  2. SearchPaths: Directories to start the search, including all subdirectories.
  3. MultiLineField: An optional field for recording multiline search results.
  4. WysiwygField: An optional WYSIWYG field for recording search results.
  5. ScanSystemDrive: A switch to set the system drive as the starting point.
  6. ScanAllDrives: A switch to set all drives, including flash drives, as the starting point.

Script Execution

  1. Parameter Validation: The script starts by validating the input parameters. It ensures that valid extensions and search paths are provided. It also dynamically sets parameters using environment variables if available.
  2. Extension and Path Preparation: It normalizes extensions and search paths by adding missing dots and backslashes. Invalid extensions and paths are identified and removed.
  3. Privilege Check: The script checks if it is running with elevated permissions, exiting if not.
  4. Search Initialization: It creates a list of search jobs for each combination of paths and extensions. Each job searches the specified path for files with the given extension.
  5. Result Collection and Processing: The script collects the search results, removes duplicates, and formats them for output. It also handles potential errors and timeouts during the search.
  6. Custom Field Setting: If specified, the script attempts to set the results in the given custom fields (MultiLineField and WysiwygField), ensuring data complies with character limits.
  7. Output and Cleanup: Finally, the script outputs the search results to the console and cleans up all jobs before exiting.

Potential Use Cases

Hypothetical Scenario

Imagine an IT professional working for a mid-sized company needing to audit all executable files on the network. By running this script with the -Extensions “.exe” parameter and specifying the network directories, they can quickly generate a detailed report of all executable files, including their paths, creation times, and sizes. This report can then be used to ensure compliance with company policies and identify unauthorized software installations.

Comparisons

Traditional Methods vs. PowerShell Script

Traditionally, finding files by extension might involve manually searching through directories or using basic command-line tools, which can be time-consuming and prone to errors. This PowerShell script automates the process, provides comprehensive reporting, and handles errors gracefully, making it far superior to manual methods or simpler scripts.

FAQs

1) What happens if no extensions are specified?

The script will exit with an error, indicating that an extension is required for the search.

2) Can I search multiple directories at once?

Yes, by providing a comma-separated list of directories in the -SearchPaths parameter, the script will search each specified directory and its subdirectories.

3) What if I need to search all drives on my system?

Using the -ScanAllDrives parameter, the script will include all available drives in the search, including flash drives.

4) How does the script handle large amounts of data?

The script includes mechanisms to truncate output if it exceeds character limits for custom fields, ensuring that results remain manageable and within system constraints.

Implications

Security and Compliance

By using this script, IT professionals can enhance security by quickly identifying potentially harmful or unauthorized files. Regular audits using this script can help maintain compliance with organizational policies and regulatory requirements.

Recommendations

  • Regular Audits: Run the script regularly to keep an up-to-date inventory of specific file types.
  • Review Logs: Always review the script’s output and logs for any errors or warnings.
  • Customize Fields: Use the custom fields to store results for better documentation and tracking.

Final Thoughts

This PowerShell script is a powerful tool for IT professionals looking to automate the task of locating and reporting files by extension. Its comprehensive functionality, combined with ease of use, makes it an essential addition to any IT toolkit. For organizations using NinjaOne, this script integrates seamlessly, further enhancing the platform’s capabilities for system management and security.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Categories:

You might also like

×

See NinjaOne in action!

By submitting this form, I accept NinjaOne's privacy policy.

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).