uninstall-hpbloat.ps1

<#PSScriptInfo
 
.VERSION 3.5
 
.GUID d4ac05f0-4b6a-437a-abcd-c7fc3d7af5fd
 
.AUTHOR zak.cole
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Uninstalls HP bloatware, installs windows updates and runs device checks
 
#>


Param(
    [switch]$devicechecks,
    [switch]$help,
    [switch]$creds
)

Clear-Host

Write-Host "
 _ _ _ _ _ _ _ _ _____ ____ _ _
| | | | (_) | | | | | | | | | __ \| _ \| | | |
| | | |_ __ _ _ __ ___| |_ __ _| | |______| |__| | |__) | |_) | | ___ __ _| |_
| | | | '_ \| | '_ \/ __| __/ _ | | |______| __ | ___/| _ <| |/ _ \ / _` | __|
| |__| | | | | | | | \__ \ || (_| | | | | | | | | | |_) | | (_) | (_| | |_
 \____/|_| |_|_|_| |_|___/\__\__,_|_|_| |_| |_|_| |____/|_|\___/ \__,_|\__| by Zak Cole
"
 -ForegroundColor blue -BackgroundColor black

if ($help) {
    Write-Host "Run this script from an elevated terminal."
    Write-Host "This script will uninstall any apps beginning with the string 'HP'"
    Write-Host "Additionally it will run windows updates and various device checks for the laptop build unless specified otherwise."
    Write-Host ""
    Write-Host "-help: Displays this help page."
    Write-Host "-devicechecks: Only run the device checks part of the script. This will script uninstalling HP software and Windows updates."
    Write-Host "-creds: Send the specified credentials to the users manager"
    Read-Host
    exit
}

if ($creds)
{
    $opt1 = Read-Host "Would you like to send credentials to the users manager? (y/n)"

if ($opt1 -like 'y')
{
    $credentials=Get-Credential


$creduser = $credentials.GetNetworkCredential().UserName
$credpass = $credentials.GetNetworkCredential().Password

$user = query user /server:$SERVER
$splitOutput = $user -split '\s+'
$splituser = $splitOutput[9]
$username = $splituser.Substring($splituser.LastIndexOf('>')+1)

$newUser = Get-ADUser -Identity $username -Properties *
$newUser.Manager
$DisplayName = $newUser.Manager.split(',')[0].trim('CN=')
$manager = $DisplayName.trim().replace(' ','.')

$smtpServer = "smtp.nhsp.local"

$senderEmail = "itsupport@nhsprofessionals.nhs.uk"
$recipientEmail = "$manager@nhsprofessionals.nhs.uk"

$subject = "New Starter Credentials"
$body = "Dear $DisplayName, `r` `r` Please see the details for you New Starter: `r` `r` Username: '$creduser' `r` Password: '$credpass' `r` `r` The Service Desk can be contacted on 0333 0144 280 for all IT related issues `r` Email the Service Desk itsupport@nhsprofessionals.nhs.uk `r` `r` Security is taken seriously here at NHSP. `r` `r` We recommend when the user starts to change their password within 2 weeks of receiving their kit for security purposes. `r` `r` Kind regards, `r` The Service Desk Team"

$mailParams = @{
    "From"       = $senderEmail
    "To"         = $recipientEmail
    "Subject"    = $subject
    "Body"       = $body
    "SmtpServer" = $smtpServer
}

Send-MailMessage @mailParams

if ($?)
    {
        Write-Host 'Mail sent successfully!'
    }
else {
    Write-Host 'There was an error sending your mail.'

    # Get-WindowsCapability -Online | Where-Object {$_.Name -like "*ActiveDirectory.DS-LDS*"} | Remove-WindowsCapability -Online
}
}
else {
    # Get-WindowsCapability -Online | Where-Object {$_.Name -like "*ActiveDirectory.DS-LDS*"} | Remove-WindowsCapability -Online
        exit
}
}

# if the $devicechecks param is specified, then only run device checks
if ($devicechecks) {

    Clear-Host

    Write-Host "Running device checks ..."
    Write-Host ""
    # Device checks

Get-VpnConnection -ErrorAction SilentlyContinue | Out-Null

if ($?)
{
    Write-Host -ForegroundColor Green -BackgroundColor black "VPN Connection: [OK]"
}
else
{
    Write-Host -ForegroundColor red -BackgroundColor black "VPN Connection: [Failed]"
}

$user = query user /server:$SERVER
$splitOutput = $user -split '\s+'
$splituser = $splitOutput[9]
$username = $splituser.Substring($splituser.LastIndexOf('>')+1)

Get-ChildItem C:\Users\$username\'OneDrive - NHS Professionals' -ErrorAction SilentlyContinue | Out-Null

if ($?)
{
    Write-Host -ForegroundColor Green -BackgroundColor black "OneDrive Connection: [OK]"
}
else
{
    Write-Host -ForegroundColor red -BackgroundColor black "OneDrive Connection: [Failed]"
}

$IntendedWinVer = '19045'
$WinVer = [System.Environment]::OSVersion.Version.build

if ($WinVer -eq $IntendedWinVer)
{
    Write-Host -ForegroundColor Green -BackgroundColor black "Windows Version: [OK]"
}
else
{
    Write-Host -ForegroundColor red -BackgroundColor black "Windows Version: [Failed]"
}

gpresult.exe /r | Out-Null

if ($?)
{
    Write-Host -ForegroundColor Green -BackgroundColor black "Group Policy: [OK]"
}
else
{
    Write-Host -ForegroundColor red -BackgroundColor black "Group Policy: [Failed]"
}
   
Write-Host ''
Write-Host 'Computer Name: '$env:COMPUTERNAME''
Write-Host 'Domain: '$env:USERDOMAIN''
$serialnumber = wmic bios get serialnumber
Write-Host ''
Write-Host $serialnumber
Write-Host ''

}

# If the $devicechecks param is not specified, then run the full script
else {
    
    Write-Host "Running full script..."
    Write-Host ""
    Write-Host "Uninstalling apps ..."

    # Uninstall all apps beggining with 'HP'
    wmic product where "name like 'HP%%'" call uninstall /nointeractive | Out-Null
    Write-Host "Checking dependencies ..."
    # Run again to clear up dependencies
    wmic product where "name like 'HP%%'" call uninstall /nointeractive | Out-Null

if ($?) {
    Write-Host "HP bloatware uninstallation completed."
    Write-Host ""
}
else {
    Write-Host "HP bloatware uninstallation failed."
    Write-Host ""
}

# If we are running as a 32-bit process on an x64 system, re-launch as a 64-bit process
if ("$env:PROCESSOR_ARCHITEW6432" -ne "ARM64")
{
    if (Test-Path "$($env:WINDIR)\SysNative\WindowsPowerShell\v1.0\powershell.exe")
    {
        & "$($env:WINDIR)\SysNative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy bypass -NoProfile -File "$PSCommandPath"
        Exit $lastexitcode
    }
}

# Create a tag file just so Intune knows this was installed
if (-not (Test-Path "$($env:ProgramData)\Microsoft\UpdateOS"))
{
    Mkdir "$($env:ProgramData)\Microsoft\UpdateOS"
}
Set-Content -Path "$($env:ProgramData)\Microsoft\UpdateOS\UpdateOS.ps1.tag" -Value "Installed"

# Start logging
Start-Transcript "$($env:ProgramData)\Microsoft\UpdateOS\UpdateOS.log"

# Main logic
$needReboot = $false
Write-Host "Installing updates."

# Load module from PowerShell Gallery
$null = Install-PackageProvider -Name NuGet -Force
$null = Install-Module PSWindowsUpdate -Force
Import-Module PSWindowsUpdate

# Install all available updates
Get-WindowsUpdate -Install -IgnoreUserInput -AcceptAll -MicrosoftUpdate -IgnoreReboot | Select-Object Title, KB, Result | Format-Table
$needReboot = (Get-WURebootStatus).RebootRequired

# Specify return code
if ($needReboot)
{
    # Set return code 3010. As long as this happens during device ESP, the computer will automatically reboot at the end of device ESP.
    Write-Host "Reboot is needed."
    Write-Host ""
    Stop-Transcript
    Exit 3010
    # Exit 1641
}
else
{
    Write-Host "No reboot is required."
    Write-Host ""
    Stop-Transcript
}

# Device checks

Get-VpnConnection -ErrorAction SilentlyContinue | Out-Null

if ($?)
{
    Write-Host -ForegroundColor Green -BackgroundColor black "VPN Connection: [OK]"
}
else
{
    Write-Host -ForegroundColor red -BackgroundColor black "VPN Connection: [Failed]"
}

$user = query user /server:$SERVER
$splitOutput = $user -split '\s+'
$splituser = $splitOutput[9]
$username = $splituser.Substring($splituser.LastIndexOf('>')+1)

Get-ChildItem C:\Users\$username\'OneDrive - NHS Professionals' | Out-Null

if ($?)
{
    Write-Host -ForegroundColor Green -BackgroundColor black "OneDrive Connection: [OK]"
}
else
{
    Write-Host -ForegroundColor red -BackgroundColor black "OneDrive Connection: [Failed]"
}

$IntendedWinVer = '19045'
$WinVer = [System.Environment]::OSVersion.Version.build

if ($WinVer -eq $IntendedWinVer)
{
    Write-Host -ForegroundColor Green -BackgroundColor black "Windows Version: [OK]"
}
else
{
    Write-Host -ForegroundColor red -BackgroundColor black "Windows Version: [Failed]"
}

gpresult.exe /r | Select-String nhsp.local | Out-Null

if ($?)
{
    Write-Host -ForegroundColor Green -BackgroundColor black "Group Policy: [OK]"
}
else
{
    Write-Host -ForegroundColor red -BackgroundColor black "Group Policy: [Failed]"
}

Write-Host ''
Write-Host 'Computer Name: '$env:COMPUTERNAME''
Write-Host 'Domain: '$env:USERDOMAIN''
$serialnumber = wmic bios get serialnumber
Write-Host ''
Write-Host $serialnumber
Write-Host ''

}