Last active
December 2, 2016 17:29
-
-
Save talatham/5773769 to your computer and use it in GitHub Desktop.
A collection of Powershell file functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Including: | |
* Deleting files and folders (incl criteria based - if older than xx days) | |
* Open and save file dialogs | |
* Hard disk usage reports |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Shows what files would be deleted from c:\windows\temp (older than 10 days) if run. Remove 'whatif' to implement. | |
filter FileAge($days) { if ( ($_.LastWriteTime -le (Get-Date).AddDays($days * -1) )) { $_ } } | |
get-childitem c:\windows\temp -recurse | FileAge 10 | del -whatif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----------- FUNCTION --------------------- | |
Function Delete-Folder($folder) | |
# Test for and delete folder. Verify folder deletion. | |
{ | |
if (Test-Path $folder) | |
{ | |
Remove-Item $folder -force -recurse | |
if (Test-Path $folder) { return "Failed to delete folder." } | |
else { return "Folder deleted." } | |
} | |
else { return "Folder not found." } | |
} | |
# ----------- USAGE --------------------- | |
$result = Delete-Folder("c:\test folder") | |
# Result returned by function - not necessarily required. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Initialise machine name | |
$server = "<>" | |
# Get fixed drive info | |
$disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3"; | |
foreach($disk in $disks) | |
{ | |
$deviceID = $disk.DeviceID; | |
[float]$size = $disk.Size; | |
[float]$freespace = $disk.FreeSpace; | |
$percentFree = [Math]::Round(($freespace / $size) * 100, 2); | |
$sizeGB = [Math]::Round($size / 1073741824, 2); | |
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2); | |
Write-Host "$server,$deviceID,$sizeGB,$freeSpaceGB,$percentFree"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------- FUNCTION ------------- | |
Function Get-FileName | |
# Open file dialog box and select a file to import | |
{ | |
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | |
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog | |
$OpenFileDialog.filter = "Text Files (*.txt)| *.txt" # Set the file types visible to dialog | |
# Alternate filters include: | |
# "CSV files (*.csv) | *.csv" | |
$OpenFileDialog.initialDirectory = "c:\" | |
$OpenFileDialog.ShowDialog() | Out-Null | |
$OpenFileDialog.filename | |
} | |
# --------- USAGE --------------- | |
$filename = Get-FileName | |
$filecontents = Get-Content $filename | |
foreach ($line in $filecontents) | |
.... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------ FUNCTION -------------- | |
Function Set-Filename | |
# Set file name for saving export | |
{ | |
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | |
$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog | |
$SaveFileDialog.Filter = "Text files (*.txt)|*.txt" | |
$SaveFileDialog.initialDirectory = "c:\" | |
if ($SaveFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) | |
{ $SaveFileDialog.FileName } | |
} | |
# ------ USAGE ------- | |
$filename = Set-FileName | |
"some output text" | out-file $filename - append |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment