Better way to Reboot a computer

I saw a post of how to use the restart-computer cmdlet. They used wait to pause the process until the computer was back online. This post is just how to take it one step further. If I want my script to wait for the reboot to occur it is normally because I want to perform more actions against it. In this post I am using the restart-computer cmdlet but I am extending it to wait but I am also specifying what it will wait For.  In this case I am waiting for WinRM to come online so that I can connect to CIM.

Yes, I splatted it because I love splatting parameters for readability and flexibility.

#Requires -Version 5
<# .SYNOPSIS     Better way to reboot computers .DESCRIPTION     Really it is just an demonstration of the flexibility of restart-computer. Being able to tell the cmdlet to     wait for a specific element to be online before continuing can help with workflows that require a reboot and     then further action. .PARAMETER ServerName     Name of the computer to be restarted. .EXAMPLE     C:\PS>.\better-rebootcomputer.ps1 -servername testmachine
    Example of how to use this cmdlet
    Created with: 	Visual Studio Code
    Created on:   	05.01.2017
    Created by:   	John Kavanagh
    Organization: 	TekSystems
    Filename:     	better-rebootcomputer.ps1
    date initials: Enter first comment here

     [Parameter(Mandatory = $true,
     ValueFromPipeline = $true,
     Position = 1,
     HelpMessage = "Enter the computer name to be rebooted")]
     [ValidateScript({test-connection -Computername $_ -Count 1 -Quiet })]
$args = @{
    ComputerName = $servername
    Wait         = $True
    For          = "WinRM"
    Force        = $True
restart-computer @args
# Now let's test - yes I am using CIM so For has to be WinRM, WMI is up first but CIM
# requires WinRM
$sess = New-CimSession -computer $servername
Get-CimInstance -classname win32_operatingsystem -CimSession $sess<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>



Delprof Alternative

So I wrote this ugly code quite some time ago. Since I have seen a few new posts in the script gallery with versions I figured I should at least post a version of what I had done a few years ago and re-used recently. Situations like shared computers and RDS can have a computer looking ragged with local profiles created on login and then not touched again. This causes capacity issues but can also cause performance issues. There used to be a utility called delprof which was great but even if it were around why not script it right?

Function clean-localprofiles {
    Param (
        [Parameter(Position = 0)]
        [int]$Days = 30,
        [string]$rptFile = "somefile name here"
        "---- Internal RDS Profile Monitor Start {0}" -f (get-date -f "MM/dd/yy hh:mm") | out-file $rptFile -Append
        Write-Warning "Filtering for user profiles older than $Days days"
        $profs = Get-CimInstance win32_userprofile -ComputerName computer1, computer2  |
        Where { $_.LastUseTime -lt $(Get-Date).Date.AddDays(- $days) -AND $_.SID.Length -gt 8 -AND $_.Loaded -eq $False }
        ForEach ($obj in $profs) {
            $uname = $obj.LocalPath.Split("\") | select -Last 1
            "Removing profile for {0} from {1} which was last used {2}" -f $uname, $obj.pscomputername, $obj.LastUseTime |
            out-file $rptFile -Append
            Try {
                Remove-CimInstance -InputObject $obj -ComputerName $obj.pscomputername
            Catch {
                "Unable to remove profile" | out-file $rptFile -Append
        # Complete
        "---- Internal RDS Profile Monitor Complete {0}" -f (get-date -f "MM/dd/yy hh:mm") |
            out-file $rptFile -Append

That’s about it. I updated the process from WMI to CIM (where possible) and tweaked based on changes to Powershell but relatively unchanged. Yeah checked my OneDrive, first script was dated in 2012…