A collection of PowerShell utility functions for file management and common administrative tasks.
Author: Sune Alexandersen Narud
Version: 1.6.0
Requires: PowerShell 5.1+, OrionDesign module (optional)
Gallery: https://www.powershellgallery.com/packages/OrionUtils
GitHub: https://github.com/suneworld/OrionUtils
From the PowerShell Gallery (recommended):
Install-Module OrionUtilsFrom GitHub (manual):
# Clone the repo and import directly
Import-Module .\OrionUtils.psd1Verify installation:
Get-Module OrionUtils
Get-Command -Module OrionUtilsOrionUtils requires the OrionDesign module for visual output functions.
Import-Module OrionDesign
Import-Module OrionUtils| Function | Description |
|---|---|
Remove-StaleFile |
Removes old files from a folder while keeping the newest N files |
Test-FileLock |
Tests if a file is locked by another process |
Format-TimeSpan |
Formats a TimeSpan to human-readable string |
Get-TimeElapsed |
Calculates and formats time elapsed since a date |
Start-Stopwatch |
Starts a named stopwatch for timing |
Stop-Stopwatch |
Stops stopwatch and records elapsed time |
Get-Stopwatch |
Gets running or completed stopwatch data |
Reset-Stopwatch |
Clears stopwatch data |
Show-StopwatchSummary |
Displays formatted performance summary |
Wait-ForInput |
Waits for key press, optionally with timer countdown |
Write-Log |
Structured logging to file |
Export-MetricsToCSV |
Export metrics to CSV with timestamps |
Get-WindowsVersion |
Converts OS version to friendly name |
Removes old files from a folder while keeping the newest ones. Supports multiple filter patterns, grouping by name patterns, and automatically skips locked files.
Note: This function was previously named
Remove-OldFiles-KeepNewest. UseRemove-StaleFilegoing forward.
Remove-StaleFile [-Path] <String> [[-Keep] <Int32>] [-Filter] <String[]>
[-NamePattern <String[]>] [-WhatIf] [-Confirm] [<CommonParameters>]| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
-Path |
String | β | β | The folder path to process |
-Keep |
Int | β | 5 | Number of newest files to keep |
-Filter |
String[] | β | β | File filter pattern(s), e.g. *.log or @('*.xlsx', '*.csv') |
-NamePattern |
String[] | β | @() | Optional filename pattern(s) to group files by |
-WhatIf |
Switch | β | β | Shows what would happen without making changes |
-Confirm |
Switch | β | β | Prompts for confirmation before each deletion |
Basic Usage - Keep 10 newest log files:
Remove-StaleFile -Path "C:\Logs" -Filter "*.log" -Keep 10Preview what would be deleted (WhatIf):
Remove-StaleFile -Path "C:\Logs" -Filter "*.log" -Keep 5 -WhatIfMultiple file types:
Remove-StaleFile -Path "C:\Output" -Filter @('*.xlsx', '*.csv') -Keep 10Group by name pattern - keep newest for EACH pattern:
Remove-StaleFile -Path "C:\Reports" -Filter "*.csv" -NamePattern @(
'DailyReport-*',
'WeeklyReport-*'
) -Keep 7This keeps the 7 newest DailyReport-*.csv AND the 7 newest WeeklyReport-*.csv files.
Tests if a file is currently locked by another process. By default, prompts the user to close the file and waits. Use -PassThru to return a boolean result instead.
Test-FileLock [-FilePath] <String> [-PassThru] [-TimeoutSeconds <Int32>] [<CommonParameters>]| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
-FilePath |
String | β | β | The full path to the file to check |
-PassThru |
Switch | β | β | Returns $true if locked, $false if not (doesn't wait) |
-TimeoutSeconds |
Int | β | 0 | Maximum seconds to wait (0 = indefinite) |
Wait for file to be available (interactive):
Test-FileLock -FilePath "C:\Data\Report.xlsx"
# If locked, prompts user to close the fileCheck lock status without waiting:
if (Test-FileLock -FilePath "C:\Data\Report.xlsx" -PassThru) {
Write-Warning "File is currently locked!"
} else {
# Safe to process the file
}Wait with timeout:
Test-FileLock -FilePath "C:\Data\Report.xlsx" -TimeoutSeconds 30Ensure file is available before writing:
$reportPath = "C:\Output\Monthly-Report.xlsx"
Test-FileLock -FilePath $reportPath
$data | Export-Excel -Path $reportPathPipeline support:
"C:\File1.xlsx", "C:\File2.xlsx" | Test-FileLock -PassThruOrionUtils integrates with OrionDesign for beautiful console output:
- Write-Action β Used for deletion confirmations
- Get-OrionTheme β Used for consistent color theming
Use PowerShell's built-in help system:
# Full help with examples
Get-Help Remove-StaleFile -Full
Get-Help Test-FileLock -Full
# Just examples
Get-Help Remove-StaleFile -Examples
# Online help (if configured)
Get-Help Remove-StaleFile -Online- π§ Removed all legacy aliases; use canonical function names only
- π₯ Removed legacy
Wait-WithKeyInterruptwrapper β useWait-ForInput -Timerdirectly - π§
Get-TimeElapsedcontinues to useFormat-TimeSpaninternally (reduces duplication) - π§
Remove-FileWithLockCheckcontinues to useTest-FileLock -Silent
- π₯ Removed legacy
Wait-WithKeyInterruptwrapper β useWait-ForInput -Timerdirectly - π§
Get-TimeElapsedcontinues to useFormat-TimeSpaninternally (reduces duplication) - π§
Remove-FileWithLockCheckcontinues to useTest-FileLock -Silent
- β¨ Consolidated
Wait-ForInputβ Added-Timerparameter for timed waits with countdown - π§
Get-TimeElapsednow usesFormat-TimeSpaninternally (reduced code duplication) - π§
Remove-FileWithLockChecknow usesTest-FileLock -Silent(reduced code duplication)
- β¨ Added Stopwatch timing system (
Start-Stopwatch,Stop-Stopwatch,Get-Stopwatch,Reset-Stopwatch,Show-StopwatchSummary) - β¨ Added
Write-Logfor structured logging - β¨ Added
Export-MetricsToCSVfor metrics export - β¨ Added
Format-TimeSpanandGet-TimeElapsedfor time operations - β¨ Added
Get-WindowsVersionfor OS version lookup
- β¨ Renamed
Remove-OldFiles-KeepNewestβRemove-StaleFile(alias removed in v1.6.0) - β¨ Added
-WhatIfand-Confirmsupport toRemove-StaleFile - β¨ Enhanced
Test-FileLockwith-PassThruand-TimeoutSeconds - β¨ Added pipeline support to
Test-FileLock - π§ Moved internal helper to Private functions
- π Complete Comment-Based Help documentation
Remove-OldFiles-KeepNewestfunctionTest-FileLockfunction
- Initial release
Remove-OldFiles-KeepNewest- File cleanup with pattern matchingTest-FileLock- File lock detection with user prompts
- OrionDesign - PowerShell UI Framework for beautiful terminal interfaces