Skip to content

Commit

Permalink
Merge pull request #23 from rstolpe/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rstolpe authored Feb 9, 2023
2 parents 0c29a00 + f473374 commit efd94c2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 104 deletions.
26 changes: 15 additions & 11 deletions .src/public/function/Remove-RSUserProfile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@
foreach ($Computer in $ComputerName) {
if (Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue) {
$AllUserProfiles = Get-CimInstance -ComputerName $Computer -className Win32_UserProfile | Where-Object { (-Not ($_.Special)) } | Select-Object LocalPath, Loaded
if ($DeleteAll -eq $True) {
if ($DeleteAll -eq $true) {
foreach ($Profile in $($AllUserProfiles)) {
if ($Profile.LocalPath.split('\')[-1] -in $Exclude) {
Write-Output "$($Profile.LocalPath.split('\')[-1]) are excluded so it wont be deleted, proceeding to next profile..."
}
else {
if ($Profile.Loaded -eq $True) {
if ($Profile.Loaded -eq "true") {
Write-Warning "The user profile $($Profile.LocalPath.split('\')[-1]) is loaded, can't delete it so skipping it!"
Continue
}
Expand All @@ -92,18 +92,22 @@
}
}
}
elseif ($DeleteAll -eq $False -and $null -ne $Delete) {
elseif ($DeleteAll -eq $false -and $null -ne $Delete) {
foreach ($user in $Delete) {
if ("$env:SystemDrive\Users\$($user)" -in $AllUserProfiles.LocalPath) {
# Add check so the profile are not loaded
try {
Write-Output "Deleting user profile $($user)..."
Get-CimInstance -ComputerName $Computer Win32_UserProfile | Where-Object { $_.LocalPath -eq "$env:SystemDrive\Users\$($user)" } | Remove-CimInstance
Write-Output "The user profile $($user) are now deleted!"
if ($Profile.LocalPath.split('\')[-1] -in $Exclude) {
Write-Output "$($Profile.LocalPath.split('\')[-1]) are excluded so it wont be deleted..."
}
catch {
Write-Error "$($PSItem.Exception)"
Continue
else {
try {
Write-Output "Deleting user profile $($user)..."
Get-CimInstance -ComputerName $Computer Win32_UserProfile | Where-Object { $_.LocalPath -eq "$env:SystemDrive\Users\$($user)" } | Remove-CimInstance
Write-Output "The user profile $($user) are now deleted!"
}
catch {
Write-Error "$($PSItem.Exception)"
Continue
}
}
}
else {
Expand Down
4 changes: 2 additions & 2 deletions DeleteUserProfile/DeleteUserProfile.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#
# Generated by: Robin Stolpe
#
# Generated on: 2023-01-19
# Generated on: 2023-02-09
#

@{
Expand All @@ -36,7 +36,7 @@
RootModule = '.\DeleteUserProfile.psm1'

# Version number of this module.
ModuleVersion = '0.1.1'
ModuleVersion = '0.1.2'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
53 changes: 17 additions & 36 deletions DeleteUserProfile/DeleteUserProfile.psm1
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
<#
MIT License
Copyright (C) 2023 Robin Stolpe.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>
Function Get-RSUserProfile {
Function Get-RSUserProfile {
<#
.SYNOPSIS
Return all user profiles that are saved on a computer
Expand Down Expand Up @@ -172,13 +149,13 @@ Function Remove-RSUserProfile {
foreach ($Computer in $ComputerName) {
if (Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue) {
$AllUserProfiles = Get-CimInstance -ComputerName $Computer -className Win32_UserProfile | Where-Object { (-Not ($_.Special)) } | Select-Object LocalPath, Loaded
if ($DeleteAll -eq $True) {
if ($DeleteAll -eq $true) {
foreach ($Profile in $($AllUserProfiles)) {
if ($Profile.LocalPath.split('\')[-1] -in $Exclude) {
Write-Output "$($Profile.LocalPath.split('\')[-1]) are excluded so it wont be deleted, proceeding to next profile..."
}
else {
if ($Profile.Loaded -eq $True) {
if ($Profile.Loaded -eq "true") {
Write-Warning "The user profile $($Profile.LocalPath.split('\')[-1]) is loaded, can't delete it so skipping it!"
Continue
}
Expand All @@ -196,18 +173,22 @@ Function Remove-RSUserProfile {
}
}
}
elseif ($DeleteAll -eq $False -and $null -ne $Delete) {
elseif ($DeleteAll -eq $false -and $null -ne $Delete) {
foreach ($user in $Delete) {
if ("$env:SystemDrive\Users\$($user)" -in $AllUserProfiles.LocalPath) {
# Add check so the profile are not loaded
try {
Write-Output "Deleting user profile $($user)..."
Get-CimInstance -ComputerName $Computer Win32_UserProfile | Where-Object { $_.LocalPath -eq "$env:SystemDrive\Users\$($user)" } | Remove-CimInstance
Write-Output "The user profile $($user) are now deleted!"
if ($Profile.LocalPath.split('\')[-1] -in $Exclude) {
Write-Output "$($Profile.LocalPath.split('\')[-1]) are excluded so it wont be deleted..."
}
catch {
Write-Error "$($PSItem.Exception)"
Continue
else {
try {
Write-Output "Deleting user profile $($user)..."
Get-CimInstance -ComputerName $Computer Win32_UserProfile | Where-Object { $_.LocalPath -eq "$env:SystemDrive\Users\$($user)" } | Remove-CimInstance
Write-Output "The user profile $($user) are now deleted!"
}
catch {
Write-Error "$($PSItem.Exception)"
Continue
}
}
}
else {
Expand All @@ -221,4 +202,4 @@ Function Remove-RSUserProfile {
Write-Output "$($Computer) are not connected to the network or it's trouble with WinRM"
}
}
}
}
1 change: 0 additions & 1 deletion test/PSScriptAnalyzer_DeleteUserProfile.psd1_2023-01-19.md

This file was deleted.

26 changes: 0 additions & 26 deletions test/PSScriptAnalyzer_DeleteUserProfile.psm1_2023-01-19.md

This file was deleted.

14 changes: 0 additions & 14 deletions test/PSScriptAnalyzer_Get-RSUserProfile_2023-01-19.md

This file was deleted.

14 changes: 0 additions & 14 deletions test/PSScriptAnalyzer_Remove-RSUserProfile_2023-01-19.md

This file was deleted.

0 comments on commit efd94c2

Please sign in to comment.