Cleanup a C: drive w/ Windows PowerShell

title

Being an overnight Administrator I get a lot of low disk space alerts when drives hit their thresholds. Tonight I’m going to put an end to all this work. I wrote the following script with little bits of help from people all over the web. I’d like to thank everyone for contributing, if it weren’t for all of you I would not know any PowerShell. (Scary Thought!)

What Does this Script do??

  1. This script will only run between 7 AM and 11 PM.
  2. Clears all code off the screen
  3.  Stops the Windows Update Service
  4. Deletes the contents of windows software distribution
  5. Deletes the contents of windows temp folder
  6. Deletes files in users temp folder
  7. Deletes temporary internet files
  8. Cleans IIS logs
  9. Empties the Recycling Bin
  10. Starts Windows update service
  11. Opens and runs Disk cleanup — must click OK in GUI 😦
  12. Outputs disk space before and after.

top

bottom

After copying all of the above code into an elevated PowerShell prompt just simply type the following:

clean

output:

bott

Note this would have cleaned up more files and given a long verbose string of files removed except I’ve been testing these cmdlets all night which has already whipped my system clean of all of these files.

Download my script here!

Kaseya Alert – Alarm Threshold 90 Pages/Sec

Image

Have you ever received an alert from Kaseya? If you answered yes, you’ve came to the right place, I too have seen thousands of tickets like the one pictured below.

Image

Luckily for us With PowerShell we can easily reverse engineer a ticket like above. Notice the ‘Log Object Name’ which in this case is ‘Pages/Sec’ with refers to memory consumption. From what I’ve noticed Kaseya monitors the specific performance counters which we can also monitor with PowerShell.

While logged onto the effected server run the following cmdlet in an Elevated PowerShell window.

(Get-Counter -ListSet Memory).Paths

Image

Notice \Memory\Pages/Sec

now run the below cmdlet to output the current \Memory\Pages/Sec.

Get-Counter \Memory\Pages/sec

Image

I like to add a little gather a little more information typically…

Hostname ; Get-Counter -Counter \Memory\Pages/sec -SampleInterval 1 -MaxSamples 3

Image

How to Upload a users Outlook\Lync image

           Image

              Image

##$root = is the only line you should really need to edit $root = [ADSI]’GC://dc=contoso,dc=int’ ## ## $SAMName=Read-Host “What is the user’s alais name? example….mkerfoot” $searcher = new-object System.DirectoryServices.DirectorySearcher($root) $searcher.filter = “(&(objectClass=user)(sAMAccountName=$SAMName))” $user = $searcher.findall() $userdn = $user[0].path $userdn = $userdn.trim(“GC”) $userdn = “LDAP” + $userdn function Select-FileDialog { param([string]$Title,[string]$Directory,[string]$Filter=”All Files (*.*)|*.*”) [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) | Out-Null $objForm = New-Object System.Windows.Forms.OpenFileDialog $objForm.InitialDirectory = $Directory $objForm.Filter = $Filter $objForm.Title = $Title $objForm.ShowHelp = $true $Show = $objForm.ShowDialog() If ($Show -eq “OK”) { Return $objForm.FileName } Else { Write-Error “Operation canceled by user.” } } $photo = Select-FileDialog -Title “Select a photo” -Directory “%userprofile%” -Filter “JPG Images (*.jpg)|*.jpg|PNG Images (*.png)|*.png” $user = [ADSI]($userdn) [byte[]]$file = Get-Content $photo -Encoding Byte # clear previous image if exist $user.Properties[“thumbnailPhoto”].Clear() # write the image to the user’s thumbnailPhoto attribute by converting the byte[] to Base64String $result = $user.Properties[“thumbnailPhoto”].Add($file) # commit the changes to AD $user.CommitChanges() if ($result -eq “0”) { Write-Host “Photo successfully uploaded.” } else

{ Write-Error “Photo was not uploaded.” }

PowerShell $PROFILE

                                   profilemaker

Where it shows { . . . } just insert your one-liners and functions there.

Here’s a one liner I just recently added to my PS $PROFILE script –> Get-Command | Get-Random | help -Examples | more

Example, if you change the 1st line to read as follows every time you open a PowerShell Prompt you will learn more powersshell cmdlets!

$NewPROFILE = {Get-Command | Get-Random | help -Examples | more}