Friday 30 November 2018

Network profile reset

The following PowerShell code is designed to reset both the local and network profile (on any number of PCs). Without PowerShell such a task is cumbersome as you need to connect to the C: drive of a PC, navigate to the 'Users' folder, check said user is logged off before renaming/deleting this folder, clear the relevant Registry key for that user ...... repeat this for each PC. Then connect to your network profile share and rename/delete the users folder.

The tool below can do all of this from the PowerShell console within a few keystrokes. It can ultimately be incorporated as a module too.

function Invoke-ProfileReset {
Removes network & local user profile.
Invoke-ProfileReset deletes the network profile of a given user.
It also renames the local user profile on a given PC.
Username of the profile the remove.
.PARAMETER ComputerName
Name of PC(s) to remove local user profile from.

Specify this switch to create a text log file of the PC
that could not be updated.
When used with -LogErrors, specifies the file path and name
to which the failed PC will be written. Defaults to
current Powershell directory.
Invoke-ProfileReset -ComputerName pc123 -User abc123
Invoke-ProfileReset -ComputerName pc123, pc456 -User abc123 -Verbose

                   HelpMessage="Username of account")]
                    HelpMessage="Computer name")]

        [string]$ErrorLog = (Get-Location).Path,

    BEGIN {
        Write-Verbose "Error log will be $ErrorLog"

        Write-Verbose 'Beginning PROCESS block'

        ForEach ($Computer in $ComputerName) {
            Write-Verbose "Checking user profile exists on $Computer"
            #Check if profile exists on remote PC
            If ($Profile = Get-WmiObject -Class win32_userprofile -ComputerName "$Computer" | Where-Object {($_.LocalPath -like "*$User
*")}) {
                Write-Verbose "$User profile exists on $Computer"

                #Check user not logged on
                If ($Profile | Where-Object {($_.Loaded -eq $false)}) {
                    #Change name of current 'User' folder to today's date/time
                    $Date = Get-Date -F "dd_MM_yyyy HH_mm_ss"
                    Rename-Item -Path "\\$Computer\C$\Users\$User" -NewName "$User.old $Date"
                    Write-Host "$User profile folder has successfully been renamed to `'$User.old DATE`' on $Computer" -ForegroundColor Green

                    #Remove user Registry key
                    $Profile | Remove-WmiObject
                    Write-Host "$User Registry key has successfully been removed on $Computer" -ForegroundColor Green

                Else {
                    Write-Host "$User is still logged on to $Computer" -ForegroundColor Red
            Else {
                Write-Host "$User profile not found on $Computer" -ForegroundColor Red


        #Display prompt to the user asking if they also wish to reset the network profile

        $NetworkCheck = Read-Host -Prompt "Would you like to reset the network profile for this user? (Y/N)"

        #If they select Yes - 'Y'
        If ($NetworkCheck -eq 'Y') {

            Try {
                $everything_ok = $true
                #Check User profile path exists
                If (!(Test-Path -Path ($NetworkProfile = "\\userfs2\profiles\$User\Windows_NT.V6"))) {
                    Write-Warning "$NetworkProfile not found"
                    $everything_ok = $false

            } Catch {
                $everything_ok = $false

                If ($LogErrors) {
                    $Computer | Out-File $ErrorLog -Append
                    Write-Warning $_.Exception.Message

            If ($everything_ok) {
                Write-Verbose "Proceeding to delete $User network profile"

                #Delete profile file and contents
                Get-ChildItem -Path "$NetworkProfile" -Force -Recurse | Remove-Item -Recurse -Force

                #Pause for a couple of seconds to complete task
                Start-Sleep -Seconds 4
                Write-Host "$NetworkProfile successfully cleaned" -ForegroundColor Green

        Else {
            Write-Verbose 'Network profile reset not requested'

        Write-Verbose 'End of PROCESS block'

    END {}


Network Profile Reset.ps1

No comments:

Post a Comment