Bra att ha scripts! [Powershell, Batch, Bash...]
Tänkte att vi i denna tråd delar med oss av scripts som man anser är "bra att ha".
Det kan vara t.ex. Powershell, Batch eller Bash.
Beskriv innan vad syftet är och vad scriptet gör.
Jag börjar.
Jag tänkte uppgradera till Windows 11 & min disk var lite för full. Hade svårt att hitta vart alla prylar låg som tog plats, så jag gjorde detta script i Powershell.
(försökte hitta någon programvara att ladda ner, men hittade ingen)
Detta skript kollar vilka folders som tar mest plats på hårddisken. Jag har exkluderat att ta med Windowsmappen.
Du får sedan en CSV-fil med mappar & dess storlekar. Alla mappar är rangordnade efter storleken på dess innehåll.
Kanske hjälper någon.
Script(Powershell)
# Define a function to format the size in MB or GB
function Format-Size {
param (
[double]$size
)
if ($size -ge 1GB) {
return "{0:N2} GB" -f ($size / 1GB)
} else {
return "{0:N2} MB" -f ($size / 1MB)
}
}
# Function to get folder size
function Get-FolderSize {
param (
[string]$folderPath
)
$size = 0
try {
$size = (Get-ChildItem -Path $folderPath -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
} catch {
Write-Warning "Cannot access folder: $folderPath"
}
return $size
}
# Recursive function to get all folders and their sizes
function Get-FoldersAndSizes {
param (
[string]$basePath
)
$folders = @()
try {
$items = Get-ChildItem -Path $basePath -Directory -ErrorAction SilentlyContinue
foreach ($item in $items) {
if ($item.FullName -notlike "C:\Windows*") {
$folderSize = Get-FolderSize -folderPath $item.FullName
$folders += [PSCustomObject]@{
Path = $item.FullName
SizeBytes = $folderSize
Size = Format-Size $folderSize
}
$folders += Get-FoldersAndSizes -basePath $item.FullName
}
}
} catch {
Write-Warning "Cannot access folder: $basePath"
}
return $folders
}
# Get the OS drive
$drive = "C:\"
# Get all folders and their sizes
$folders = Get-FoldersAndSizes -basePath $drive
# Sort the results by size in descending order
$sortedFolders = $folders | Sort-Object -Property SizeBytes -Descending
# Output the results
$sortedFolders | Select-Object -Property Path, Size | Format-Table -AutoSize
# Export the results to CSV file
$sortedFolders | Select-Object -Property Path, Size | Export-Csv -Path "C:\folder_sizes.csv" -NoTypeInformation
Ändra variabeln $drive till den disk du vill kolla.
OBS. Skriptet tar lång tid att köra.
På min 180 Gb disk tog det 15 minuter.
[IT-Dept]
Ryzen 1700 OC - 32 - 1070