Skip to content

Instantly share code, notes, and snippets.

@biacz
Created August 14, 2019 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biacz/db86dc45e93e5831cc1c3c94c9d115c4 to your computer and use it in GitHub Desktop.
Save biacz/db86dc45e93e5831cc1c3c94c9d115c4 to your computer and use it in GitHub Desktop.
diskspaceanalyzer
function CleanVariables { Get-Variable | Where-Object { $startupVariables -notcontains $_.Name } | ForEach-Object {
try { Remove-Variable -Name "$($_.Name)" -Force -Scope "global" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue }
catch { }
}
}
CleanVariables
<# This form was created using POSHGUI.com a free online gui designer for PowerShell
.NAME
Disk Space Analyzer
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '700,800'
$Form.text = "Form"
$Form.TopMost = $false
$TB_ComputerName = New-Object system.Windows.Forms.TextBox
$TB_ComputerName.multiline = $false
$TB_ComputerName.width = 219
$TB_ComputerName.height = 20
$TB_ComputerName.location = New-Object System.Drawing.Point(147,145)
$TB_ComputerName.Font = 'Microsoft Sans Serif,10'
$LB_ComputerName = New-Object system.Windows.Forms.Label
$LB_ComputerName.text = "Computer Name:"
$LB_ComputerName.AutoSize = $true
$LB_ComputerName.width = 25
$LB_ComputerName.height = 10
$LB_ComputerName.location = New-Object System.Drawing.Point(34,148)
$LB_ComputerName.Font = 'Microsoft Sans Serif,10'
$TB_UserName = New-Object system.Windows.Forms.TextBox
$TB_UserName.multiline = $false
$TB_UserName.width = 219
$TB_UserName.height = 20
$TB_UserName.location = New-Object System.Drawing.Point(147,172)
$TB_UserName.Font = 'Microsoft Sans Serif,10'
$LB_UserName = New-Object system.Windows.Forms.Label
$LB_UserName.text = "User Name:"
$LB_UserName.AutoSize = $true
$LB_UserName.width = 25
$LB_UserName.height = 10
$LB_UserName.location = New-Object System.Drawing.Point(34,175)
$LB_UserName.Font = 'Microsoft Sans Serif,10'
$LB_INFO = New-Object system.Windows.Forms.Label
$LB_INFO.text = "Please enter the Computer Name and User Name below and press `"RUN!`""
$LB_INFO.AutoSize = $true
$LB_INFO.width = 25
$LB_INFO.height = 10
$LB_INFO.location = New-Object System.Drawing.Point(34,81)
$LB_INFO.Font = 'Microsoft Sans Serif,10'
$LB_TITLE = New-Object system.Windows.Forms.Label
$LB_TITLE.text = "DISK SPACE ANALYZER v0.1b"
$LB_TITLE.AutoSize = $true
$LB_TITLE.width = 25
$LB_TITLE.height = 10
$LB_TITLE.location = New-Object System.Drawing.Point(137,20)
$LB_TITLE.Font = 'Microsoft Sans Serif,20'
$BTN_RUN = New-Object system.Windows.Forms.Button
$BTN_RUN.text = "RUN!"
$BTN_RUN.width = 60
$BTN_RUN.height = 30
$BTN_RUN.location = New-Object System.Drawing.Point(147,209)
$BTN_RUN.Font = 'Microsoft Sans Serif,10'
$BTN_Cancel = New-Object system.Windows.Forms.Button
$BTN_Cancel.text = "CLOSE"
$BTN_Cancel.width = 60
$BTN_Cancel.height = 30
$BTN_Cancel.location = New-Object System.Drawing.Point(223,209)
$BTN_Cancel.Font = 'Microsoft Sans Serif,10'
$LB_Status = New-Object system.Windows.Forms.Label
$LB_Status.text = "Status: "
$LB_Status.AutoSize = $true
$LB_Status.width = 25
$LB_Status.height = 10
$LB_Status.location = New-Object System.Drawing.Point(34,113)
$LB_Status.Font = 'Microsoft Sans Serif,10,style=Bold'
$LB_StatusUpdate = New-Object system.Windows.Forms.Label
$LB_StatusUpdate.AutoSize = $true
$LB_StatusUpdate.width = 25
$LB_StatusUpdate.height = 10
$LB_StatusUpdate.location = New-Object System.Drawing.Point(85,113)
$LB_StatusUpdate.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(@($TB_ComputerName,$LB_ComputerName,$TB_UserName,$LB_UserName,$LB_INFO,$LB_TITLE,$BTN_RUN,$BTN_Cancel,$LB_Status,$LB_StatusUpdate))
$BTN_Cancel.Add_Click({ closeForm })
$BTN_RUN.Add_Click({ RunCalculation })
#Write your logic code here
Add-Type -AssemblyName System.Windows.Forms.DataVisualization
$Chart = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Size = '750,600'
$ChartArea = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisX.Title = 'Directory'
$ChartArea.AxisY.Title = 'SizeGB'
$ChartArea.AxisX.Interval = '1'
$ChartArea.AxisX.LabelStyle.Enabled = $true
$ChartArea.AxisX.LabelStyle.Angle = 90
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series.Add('SizeGB')
$Title = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($Title)
$Chart.Titles[0].Text = 'Folder Size Analyzer'
$Chart.Series['SizeGB'].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::StackedBar
function closeForm { $Form.close() }
function RunCalculation {
$computer = $TB_ComputerName.Text
$user = $TB_UserName.Text
$LB_StatusUpdate.Text = "Running Disk Space Query for $user on $computer..."
Start-Sleep 3
$LB_StatusUpdate.Text = "Do not close this window!"
Start-Sleep 3
$test = get-childitem -force "\\$computer\c$\users\$user" -ErrorAction SilentlyContinue | where-object { $_ -is [io.directoryinfo] } |
foreach-object {
$len = 0
get-childitem -recurse -force $_.FullName -ErrorAction SilentlyContinue | foreach-object { $len += $_.length }
[pscustomobject]@{
Name = $_.FullName
SizeGB = [math]::round($len / 1Gb, 2)
}
}
$LB_StatusUpdate.Text = "Creating Report..."
$test | sort-object SizeGB | foreach-object {
$Value = $Chart.Series['SizeGB'].Points.AddXY($_.Name,$_.SizeGB)
}
$Chart.Left = 20
$Chart.Top = 240
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Left -bor
$Form.controls.add($Chart)
}
$Form.Add_Shown({ $Form.Activate() })
$result = $Form.ShowDialog()
if ($result –eq [System.Windows.Forms.DialogResult]::Cancel)
{
write-output 'User pressed cancel'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment