Welcome › Forums › General PowerShell Q&A › Help!! Modify Script Remote Profile deleting
- This topic has 4 replies, 4 voices, and was last updated 10 months, 3 weeks ago by
Participant.
Viewing 4 reply threads
-
AuthorPosts
-
-
March 4, 2020 at 1:40 pm #207909
Hi All,
Hope you can help I have the below script that can delete profiles 1 by 1 looking to add an option to delete all profiles rather as an additional option could anyone help
PowerShell1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374#Prompt for a computer to connect to$computer = Read-Host “Please enter a computer name”#Test network connection before making connectionIf ($computer -ne $Env:Computername) {If (!(Test-Connection -comp $computer -count 1 -quiet)) {Write-Warning “$computer is not accessible, please try a different computer or verify it is powered on.”Break}}Try {# #Verify that the OS Version is 6.0 and above, otherwise the script will fail# If ((Get-WmiObject -ComputerName $computer Win32_OperatingSystem -ea stop).Version -lt 6.0) {# Write-Warning “The Operating System of the computer is not supported.nClient: Vista and abovenServer: Windows 2008 and above.”# Break# }}Catch {Write-Warning “$($error[0])”Break}Do {#Gather all of the user profiles on computerTry {[array]$users = Get-WmiObject -ComputerName $computer Win32_UserProfile -filter “LocalPath Like ‘C:\\Users\\%’” -ea stop}Catch {Write-Warning “$($error[0]) ”Break}#Cache the number of users$num_users = $users.countWrite-Host -ForegroundColor Green “User profiles on $($computer):”#Begin iterating through all of the accounts to displayFor ($i=0;$i -lt $num_users; $i++) {Write-Host -ForegroundColor Green “$($i): $(($users[$i].localpath).replace(‘C:\Users\’,”))”}Write-Host -ForegroundColor Green “q: Quit”#Prompt for user to select a profile to remove from computerDo {$account = Read-Host “Select a number to delete local profile or ‘q’ to quit”#Find out if user selected to quit, otherwise answer is an integerIf ($account -NotLike “q*”) {$account = $account -as [int]}}#Ensure that the selection is a number and within the valid rangeUntil (($account -lt $num_users -AND $account -match “\d”) -OR $account -Like “q*”)If ($account -Like “q*”) {Break}Write-Host -ForegroundColor Yellow “Deleting profile: $(($users[$account].localpath).replace(‘C:\Users\’,”))”#Remove the local profile($users[$account]).Delete()Write-Host -ForegroundColor Green “Profile: $(($users[$account].localpath).replace(‘C:\Users\’,”)) has been deleted”#Configure yes choice$yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”,”Remove another profile.”#Configure no choice$no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”,”Quit profile removal”#Determine Values for Choice$choice = [System.Management.Automation.Host.ChoiceDescription[]] @($yes,$no)#Determine Default Selection[int]$default = 0#Present choice option to user$userchoice = $host.ui.PromptforChoice(“”,”Remove Another Profile?”,$choice,$default)}#If user selects No, then quit the scriptUntil ($userchoice -eq 1)-
This topic was modified 10 months, 3 weeks ago by
kvprasoon. Reason: code formatting
-
This topic was modified 10 months, 3 weeks ago by
-
March 4, 2020 at 4:27 pm #207951
before reading the code, we would like to know the issue you are facing here 🙂
-
March 4, 2020 at 4:30 pm #207960
Thanks for the response 🙂
So I need a option to delete all profiles rather than selecting 1 by 1 cant seem to find any script that works with remote access that can do this
-
March 4, 2020 at 4:41 pm #207978
Have you tried to modify your script? Looks like you can just remove all the user prompt lines/loops (most of your script) and put line 55-56 in a simple foreach loop.
-
March 4, 2020 at 5:45 pm #208041
Provided a basic answer in your other post:
-
-
AuthorPosts
Viewing 4 reply threads
- The topic ‘Help!! Modify Script Remote Profile deleting’ is closed to new replies.