Skip to content

Commit

Permalink
Merge pull request #9 from rstolpe/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
rstolpe authored Dec 3, 2022
2 parents 90def61 + 0396720 commit d5c80b3
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .src/DeleteUserProfile.psd1.source
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
ReleaseNotes = 'https://github.com/rstolpe/DeleteUserProfile/releases'

# Prerelease string of this module
Prerelease = 'beta'
# Prerelease = 'beta2'

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
RequireLicenseAcceptance = $false
Expand Down
8 changes: 4 additions & 4 deletions .src/public/function/Get-RSUserProfile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# This will return all of the user profiles saved on the local machine
.EXAMPLE
Get-RSUserProfile -ExcludedProfile "Frank, rstolpe"
Get-RSUserProfile -Excluded "Frank, rstolpe"
# This will return all of the user profiles saved on the local machine except user profiles that are named Frank and rstolpe
.EXAMPLE
Expand All @@ -30,7 +30,7 @@
# This will return all of the user profiles saved on the remote computers named Win11-Test and Win10
.EXAMPLE
Get-RSUserProfile -ComputerName "Win11-Test" -ExcludedProfile "Frank, rstolpe"
Get-RSUserProfile -ComputerName "Win11-Test" -Excluded "Frank, rstolpe"
# This will return all of the user profiles saved on the remote computer "Win11-Test" except user profiles that are named Frank and rstolpe
.LINK
Expand All @@ -51,14 +51,14 @@
[Parameter(Mandatory = $false, HelpMessage = "Enter computername on the computer that you to delete user profiles from, multiple names are accepted if separated with ,")]
[string]$ComputerName = "localhost",
[Parameter(Mandatory = $false, HelpMessage = "Enter name of user profiles that you want to exclude, multiple input are accepted if separated with ,")]
[string]$ExcludedProfile
[string]$Excluded
)
foreach ($Computer in $ComputerName.Split(",").Trim()) {
if (Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue) {
Write-Output "`n== All profiles on $($Computer) ==`n"
try {
Get-CimInstance -ComputerName $Computer -className Win32_UserProfile | Where-Object { (-Not ($_.Special)) } | Foreach-Object {
if (-Not ($_.LocalPath.split('\')[-1] -in $ExcludedProfile)) {
if (-Not ($_.LocalPath.split('\')[-1] -in $Excluded)) {
[PSCustomObject]@{
'UserName' = $_.LocalPath.split('\')[-1]
'Profile path' = $_.LocalPath
Expand Down
29 changes: 15 additions & 14 deletions .src/public/function/Remove-RSUserProfile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
Let you delete user profiles from a local computer or remote computer, you can also delete all of the user profiles. You can also exclude profiles.
If the profile are loaded you can't delete it. The special Windows profiles are excluded
.PARAMETER Computer
.PARAMETER ComputerName
The name of the remote computer you want to display all of the user profiles from. If you want to use it on a local computer you don't need to fill this one out.
.PARAMETER ProfileToDelete
.PARAMETER Delete
If you want to delete just one user profile your specify the username here.
.PARAMETER DeleteAll
Expand All @@ -21,23 +21,23 @@
# This will delete all of the user profiles from the local computer your running the script from.
.EXAMPLE
Remove-RSUserProfile -ExcludedProfile "User1, User2" -DeleteAll
Remove-RSUserProfile -Excluded "User1, User2" -DeleteAll
# This will delete all of the user profiles except user profile User1 and User2 on the local computer
.EXAMPLE
Remove-RSUserProfile -ProfileToDelete "User1, User2"
Remove-RSUserProfile -Delete "User1, User2"
# This will delete only user profile "User1" and "User2" from the local computer where you run the script from.
.EXAMPLE
Remove-RSUserProfile -ComputerName "Win11-test" -DeleteAll
# This will delete all of the user profiles on the remote computer named "Win11-Test"
.EXAMPLE
Remove-RSUserProfile -ComputerName "Win11-test" -ExcludedProfile "User1, User2" -DeleteAll
Remove-RSUserProfile -ComputerName "Win11-test" -Excluded "User1, User2" -DeleteAll
# This will delete all of the user profiles except user profile User1 and User2 on the remote computer named "Win11-Test"
.EXAMPLE
Remove-RSUserProfile -ComputerName "Win11-test" -ProfileToDelete "User1, User2"
Remove-RSUserProfile -ComputerName "Win11-test" -Delete "User1, User2"
# This will delete only user profile "User1" and "User2" from the remote computer named "Win11-Test"
.LINK
Expand All @@ -58,17 +58,18 @@
[Parameter(Mandatory = $false, HelpMessage = "Enter computername on the computer that you to delete user profiles from, multiple names are accepted if separated with ,")]
[string]$ComputerName = "localhost",
[Parameter(Mandatory = $false, HelpMessage = "Enter the userprofile that you want to delete")]
[string]$ProfileToDelete,
[string]$Delete,
[Parameter(Mandatory = $false, HelpMessage = "Use if you want to delete all user profiles")]
[switch]$DeleteAll = $false,
[Parameter(Mandatory = $false, HelpMessage = "Enter name of user profiles that you want to exclude, multiple input are accepted if separated with ,")]
[string]$ExcludedProfile
[string]$Excluded
)

foreach ($Computer in $ComputerName.Split(",").Trim()) {
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) {
foreach ($Profile in $(Get-CimInstance -ComputerName $Computer -className Win32_UserProfile | Where-Object { (-Not ($_.Special)) } | Select-Object LocalPath, Loaded)) {
foreach ($Profile in $($AllUserProfiles)) {
if ($Profile.LocalPath.split('\')[-1] -in $ExcludedProfile) {
Write-Output "$($Profile.LocalPath.split('\')[-1]) are excluded so it wont be deleted, proceeding to next profile..."
}
Expand All @@ -81,7 +82,7 @@
try {
Write-Output "Deleting user profile $($Profile.LocalPath.split('\')[-1])..."
Get-CimInstance -ComputerName $Computer Win32_UserProfile | Where-Object { $_.LocalPath -eq $Profile.LocalPath } | Remove-CimInstance
Write-Output "The user profile $($Profile.LocalPath.split('\')[-1]) are now deleted!" -ForegroundColor Green
Write-Output "The user profile $($Profile.LocalPath.split('\')[-1]) are now deleted!"
}
catch {
Write-Error "$($PSItem.Exception)"
Expand All @@ -91,14 +92,14 @@
}
}
}
elseif ($DeleteAll -eq $False) {
foreach ($user in $ProfileToDelete.Split(",").Trim()) {
elseif ($DeleteAll -eq $False -and $null -ne $Delete) {
foreach ($user in $Delete.Split(",").Trim()) {
if ("$env:SystemDrive\Users\$($user)" -in $AllUserProfiles.LocalPath) {
# check if the userprofile are loaded and if it is show warning
# 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!" -ForegroundColor Green
Write-Output "The user profile $($user) are now deleted!"
}
catch {
Write-Error "$($PSItem.Exception)"
Expand Down
2 changes: 1 addition & 1 deletion DeleteUserProfile/DeleteUserProfile.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
ReleaseNotes = 'https://github.com/rstolpe/DeleteUserProfile/releases'

# Prerelease string of this module
Prerelease = 'beta'
# Prerelease = 'beta2'

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
RequireLicenseAcceptance = $false
Expand Down
37 changes: 19 additions & 18 deletions DeleteUserProfile/DeleteUserProfile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Function Get-RSUserProfile {
# This will return all of the user profiles saved on the local machine
.EXAMPLE
Get-RSUserProfile -ExcludedProfile "Frank, rstolpe"
Get-RSUserProfile -Excluded "Frank, rstolpe"
# This will return all of the user profiles saved on the local machine except user profiles that are named Frank and rstolpe
.EXAMPLE
Expand All @@ -45,7 +45,7 @@ Function Get-RSUserProfile {
# This will return all of the user profiles saved on the remote computers named Win11-Test and Win10
.EXAMPLE
Get-RSUserProfile -ComputerName "Win11-Test" -ExcludedProfile "Frank, rstolpe"
Get-RSUserProfile -ComputerName "Win11-Test" -Excluded "Frank, rstolpe"
# This will return all of the user profiles saved on the remote computer "Win11-Test" except user profiles that are named Frank and rstolpe
.LINK
Expand All @@ -66,14 +66,14 @@ Function Get-RSUserProfile {
[Parameter(Mandatory = $false, HelpMessage = "Enter computername on the computer that you to delete user profiles from, multiple names are accepted if separated with ,")]
[string]$ComputerName = "localhost",
[Parameter(Mandatory = $false, HelpMessage = "Enter name of user profiles that you want to exclude, multiple input are accepted if separated with ,")]
[string]$ExcludedProfile
[string]$Excluded
)
foreach ($Computer in $ComputerName.Split(",").Trim()) {
if (Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue) {
Write-Output "`n== All profiles on $($Computer) ==`n"
try {
Get-CimInstance -ComputerName $Computer -className Win32_UserProfile | Where-Object { (-Not ($_.Special)) } | Foreach-Object {
if (-Not ($_.LocalPath.split('\')[-1] -in $ExcludedProfile)) {
if (-Not ($_.LocalPath.split('\')[-1] -in $Excluded)) {
[PSCustomObject]@{
'UserName' = $_.LocalPath.split('\')[-1]
'Profile path' = $_.LocalPath
Expand Down Expand Up @@ -102,10 +102,10 @@ Function Remove-RSUserProfile {
Let you delete user profiles from a local computer or remote computer, you can also delete all of the user profiles. You can also exclude profiles.
If the profile are loaded you can't delete it. The special Windows profiles are excluded
.PARAMETER Computer
.PARAMETER ComputerName
The name of the remote computer you want to display all of the user profiles from. If you want to use it on a local computer you don't need to fill this one out.
.PARAMETER ProfileToDelete
.PARAMETER Delete
If you want to delete just one user profile your specify the username here.
.PARAMETER DeleteAll
Expand All @@ -116,23 +116,23 @@ Function Remove-RSUserProfile {
# This will delete all of the user profiles from the local computer your running the script from.
.EXAMPLE
Remove-RSUserProfile -ExcludedProfile "User1, User2" -DeleteAll
Remove-RSUserProfile -Excluded "User1, User2" -DeleteAll
# This will delete all of the user profiles except user profile User1 and User2 on the local computer
.EXAMPLE
Remove-RSUserProfile -ProfileToDelete "User1, User2"
Remove-RSUserProfile -Delete "User1, User2"
# This will delete only user profile "User1" and "User2" from the local computer where you run the script from.
.EXAMPLE
Remove-RSUserProfile -ComputerName "Win11-test" -DeleteAll
# This will delete all of the user profiles on the remote computer named "Win11-Test"
.EXAMPLE
Remove-RSUserProfile -ComputerName "Win11-test" -ExcludedProfile "User1, User2" -DeleteAll
Remove-RSUserProfile -ComputerName "Win11-test" -Excluded "User1, User2" -DeleteAll
# This will delete all of the user profiles except user profile User1 and User2 on the remote computer named "Win11-Test"
.EXAMPLE
Remove-RSUserProfile -ComputerName "Win11-test" -ProfileToDelete "User1, User2"
Remove-RSUserProfile -ComputerName "Win11-test" -Delete "User1, User2"
# This will delete only user profile "User1" and "User2" from the remote computer named "Win11-Test"
.LINK
Expand All @@ -153,17 +153,18 @@ Function Remove-RSUserProfile {
[Parameter(Mandatory = $false, HelpMessage = "Enter computername on the computer that you to delete user profiles from, multiple names are accepted if separated with ,")]
[string]$ComputerName = "localhost",
[Parameter(Mandatory = $false, HelpMessage = "Enter the userprofile that you want to delete")]
[string]$ProfileToDelete,
[string]$Delete,
[Parameter(Mandatory = $false, HelpMessage = "Use if you want to delete all user profiles")]
[switch]$DeleteAll = $false,
[Parameter(Mandatory = $false, HelpMessage = "Enter name of user profiles that you want to exclude, multiple input are accepted if separated with ,")]
[string]$ExcludedProfile
[string]$Excluded
)

foreach ($Computer in $ComputerName.Split(",").Trim()) {
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) {
foreach ($Profile in $(Get-CimInstance -ComputerName $Computer -className Win32_UserProfile | Where-Object { (-Not ($_.Special)) } | Select-Object LocalPath, Loaded)) {
foreach ($Profile in $($AllUserProfiles)) {
if ($Profile.LocalPath.split('\')[-1] -in $ExcludedProfile) {
Write-Output "$($Profile.LocalPath.split('\')[-1]) are excluded so it wont be deleted, proceeding to next profile..."
}
Expand All @@ -176,7 +177,7 @@ Function Remove-RSUserProfile {
try {
Write-Output "Deleting user profile $($Profile.LocalPath.split('\')[-1])..."
Get-CimInstance -ComputerName $Computer Win32_UserProfile | Where-Object { $_.LocalPath -eq $Profile.LocalPath } | Remove-CimInstance
Write-Output "The user profile $($Profile.LocalPath.split('\')[-1]) are now deleted!" -ForegroundColor Green
Write-Output "The user profile $($Profile.LocalPath.split('\')[-1]) are now deleted!"
}
catch {
Write-Error "$($PSItem.Exception)"
Expand All @@ -186,14 +187,14 @@ Function Remove-RSUserProfile {
}
}
}
elseif ($DeleteAll -eq $False) {
foreach ($user in $ProfileToDelete.Split(",").Trim()) {
elseif ($DeleteAll -eq $False -and $null -ne $Delete) {
foreach ($user in $Delete.Split(",").Trim()) {
if ("$env:SystemDrive\Users\$($user)" -in $AllUserProfiles.LocalPath) {
# check if the userprofile are loaded and if it is show warning
# 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!" -ForegroundColor Green
Write-Output "The user profile $($user) are now deleted!"
}
catch {
Write-Error "$($PSItem.Exception)"
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ If you use this module on a remote computer you need to make sure that you have
* [Webpage/Blog](https://www.stolpe.io)
* [Twitter](https://twitter.com/rstolpes)
* [LinkedIn](https://www.linkedin.com/in/rstolpe/)
* [YouTube]()
* [PowerShell Gallery](https://www.powershellgallery.com/profiles/rstolpe)

# Help
Expand Down Expand Up @@ -51,7 +50,7 @@ Get-RSUserProfile
Return all user profiles that are saved on the local computer

```
Get-RSUserProfile -ExcludedProfile "Frank, rstolpe"
Get-RSUserProfile -Excluded "Frank, rstolpe"
```
This will return all of the user profiles saved on the local machine except user profiles that are named Frank and rstolpe

Expand All @@ -69,11 +68,11 @@ Remove-RSUserProfile -DeleteAll
This will delete all of the user profiles from the localhost / computer your running the module from.

```
Remove-RSUserProfile -ExcludedProfile "User1, User2" -DeleteAll
Remove-RSUserProfile -Excluded "User1, User2" -DeleteAll
```
This will delete all of the user profiles except user profile User1 and User2 on the local computer

```
Remove-RSUserProfile -ProfileToDelete "User1, User2"
Remove-RSUserProfile -Delete "User1, User2"
```
This will delete only user profile "User1" and "User2" from the local computer where you run the script from.
8 changes: 4 additions & 4 deletions help/Get-RSUserProfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SYNOPSIS


SYNTAX
Get-RSUserProfile [[-ComputerName] <String>] [[-ExcludedProfile] <String>] [<CommonParameters>]
Get-RSUserProfile [[-ComputerName] <String>] [[-Excluded] <String>] [<CommonParameters>]


DESCRIPTION
Expand All @@ -25,7 +25,7 @@ PARAMETERS
Accept pipeline input? false
Accept wildcard characters? false

-ExcludedProfile <String>
-Excluded <String>

Required? false
Position? 2
Expand Down Expand Up @@ -66,7 +66,7 @@ NOTES

-------------------------- EXAMPLE 2 --------------------------

PS > Get-RSUserProfile -ExcludedProfile "Frank, rstolpe"
PS > Get-RSUserProfile -Excluded "Frank, rstolpe"
# This will return all of the user profiles saved on the local machine except user profiles that are named Frank and rstolpe


Expand Down Expand Up @@ -96,7 +96,7 @@ NOTES

-------------------------- EXAMPLE 5 --------------------------

PS > Get-RSUserProfile -ComputerName "Win11-Test" -ExcludedProfile "Frank, rstolpe"
PS > Get-RSUserProfile -ComputerName "Win11-Test" -Excluded "Frank, rstolpe"
# This will return all of the user profiles saved on the remote computer "Win11-Test" except user profiles that are named Frank and rstolpe


Expand Down
Loading

0 comments on commit d5c80b3

Please sign in to comment.