#Requires -RunAsAdministrator ######################HELPER FUNCTIONS#################################### # Ask a Yes/No question. Ask again until the user gives a valid answer. function Get-YesNo { param( [string]$Prompt, [bool]$Default ) while ($true) { $Answer = Read-Host -Prompt $Prompt if ([string]::IsNullOrWhiteSpace($Answer)) { return $Default } switch -Regex ($Answer.Trim()) { '^(y|yes)$' { return $true } '^(n|no)$' { return $false } default { Write-Host 'Type Yes or No.' -ForegroundColor Yellow } } } } # Ask for a whole number of minutes. Ask again until the user gives a valid number. function Get-Minutes { param( [string]$Prompt, [int]$Default ) while ($true) { $Answer = Read-Host -Prompt $Prompt if ([string]::IsNullOrWhiteSpace($Answer)) { return $Default } $Number = 0 if ([int]::TryParse($Answer.Trim(), [ref]$Number) -and $Number -ge 0 -and $Number -le 9999) { return $Number } Write-Host 'Type a whole number from 0 to 9999.' -ForegroundColor Yellow } } # Install one app with winget. Report a warning if winget fails. function Install-WingetApp { param([string]$Id) Write-Host "Installing $Id ..." -ForegroundColor Cyan winget install --id $Id --exact --silent --source winget ` --accept-package-agreements --accept-source-agreements if ($LASTEXITCODE -ne 0) { Write-Warning "winget returned exit code $LASTEXITCODE for $Id. Check the output above." } } # Search, download, and install Windows updates with the built-in Windows Update API. # The function returns $true when Windows needs a restart. function Install-WindowsUpdates { Write-Host 'Searching for Windows updates. This step can take several minutes...' -ForegroundColor Cyan $Session = New-Object -ComObject Microsoft.Update.Session $Searcher = $Session.CreateUpdateSearcher() try { $SearchResult = $Searcher.Search("IsInstalled=0 and IsHidden=0 and Type='Software'") } catch { Write-Warning "The Windows Update search failed: $($_.Exception.Message)" return $false } if ($SearchResult.Updates.Count -eq 0) { Write-Host 'Windows Update found no new updates.' -ForegroundColor Green return $false } $ToDownload = New-Object -ComObject Microsoft.Update.UpdateColl foreach ($Update in $SearchResult.Updates) { if (-not $Update.EulaAccepted) { $Update.AcceptEula() } Write-Host " - $($Update.Title)" [void]$ToDownload.Add($Update) } Write-Host "Downloading $($ToDownload.Count) update(s)..." -ForegroundColor Cyan $Downloader = $Session.CreateUpdateDownloader() $Downloader.Updates = $ToDownload [void]$Downloader.Download() $ToInstall = New-Object -ComObject Microsoft.Update.UpdateColl foreach ($Update in $ToDownload) { if ($Update.IsDownloaded) { [void]$ToInstall.Add($Update) } } if ($ToInstall.Count -eq 0) { Write-Warning 'Windows Update downloaded no updates. Check the network connection.' return $false } Write-Host "Installing $($ToInstall.Count) update(s)..." -ForegroundColor Cyan $Installer = $Session.CreateUpdateInstaller() $Installer.Updates = $ToInstall $InstallResult = $Installer.Install() # Result codes: 2 = Succeeded, 3 = Succeeded with errors, 4 = Failed, 5 = Aborted $ResultNames = @{ 0 = 'Not started'; 1 = 'In progress'; 2 = 'Succeeded'; 3 = 'Succeeded with errors'; 4 = 'Failed'; 5 = 'Aborted' } for ($i = 0; $i -lt $ToInstall.Count; $i++) { $Code = $InstallResult.GetUpdateResult($i).ResultCode Write-Host " [$($ResultNames[$Code])] $($ToInstall.Item($i).Title)" } Write-Host "Overall Windows Update result: $($ResultNames[$InstallResult.ResultCode])" -ForegroundColor Cyan return $InstallResult.RebootRequired } ######################CHECK FOR WINGET#################################### if (-not (Get-Command winget -ErrorAction SilentlyContinue)) { Write-Host 'winget is not available on this PC.' -BackgroundColor Black -ForegroundColor Red Write-Host 'Install or update "App Installer" from the Microsoft Store, then run this script again.' -ForegroundColor Red exit 1 } winget source update #Set Hostname Write-Host 'Enter a new Hostname or leave blank to use the Default [ENTER]' -BackgroundColor Black -ForegroundColor Yellow Try { Rename-Computer } Catch { Write-Host "The Hostname is $env:COMPUTERNAME" -BackgroundColor Black -ForegroundColor Yellow } #cd to Downloads Set-Location ~\Downloads ######################QUESTIONS########################################## $InstallReader = Get-YesNo -Prompt 'Do you want to install Adobe Reader? (Y/n) default [Y]' -Default $true $InstallLibre = Get-YesNo -Prompt 'Do you want to install Libreoffice? (Y/n) default [n]' -Default $false $NeedIDLink = Get-YesNo -Prompt 'Does this PC need a Intelli Dealer shortcut? (Y/n) default [n]' -Default $false $InstallSC = Get-YesNo -Prompt 'Do you want to install ScreenConnect? (Y/n) default [Y]' -Default $true $RunUpdates = Get-YesNo -Prompt 'Do you want to run Windows Update at the end? (Y/n) default [Y]' -Default $true $LockMinutes = Get-Minutes -Prompt 'How many minutes of inactivity before the screen locks? (0 = never) default [15]' -Default 15 ######################POWER AND LOCK SETTINGS############################ # AC power (plugged in): never sleep or hibernate. Write-Host 'Setting power options...' -ForegroundColor Cyan powercfg /change standby-timeout-ac 0 powercfg /change hibernate-timeout-ac 0 # Battery: turn off the display after 20 minutes. Sleep after 30 minutes. # Desktop PCs have no battery, so Windows ignores these battery settings. powercfg /change monitor-timeout-dc 20 powercfg /change standby-timeout-dc 30 powercfg /change hibernate-timeout-dc 0 # Set the screen lock time. Windows calls this the "Machine inactivity limit". $LockKey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System' if ($LockMinutes -eq 0) { Remove-ItemProperty -Path $LockKey -Name 'InactivityTimeoutSecs' -ErrorAction SilentlyContinue Write-Host 'The screen will NOT lock automatically.' -ForegroundColor Yellow } else { Set-ItemProperty -Path $LockKey -Name 'InactivityTimeoutSecs' -Value ($LockMinutes * 60) -Type DWord Write-Host "The screen will lock after $LockMinutes minute(s) of inactivity. This takes effect after a restart." -ForegroundColor Green } #Adobe Reader if ($InstallReader) { Install-WingetApp 'Adobe.Acrobat.Reader.64-bit' } else { Write-Host 'Adobe Reader NOT Installed' } #LibreOffice if ($InstallLibre) { Install-WingetApp 'TheDocumentFoundation.LibreOffice' } else { Write-Host 'Libre Office NOT Installed' } #IntelliDealer shortcut if ($NeedIDLink) { $Shell = New-Object -ComObject ("WScript.Shell") $Favorite = $Shell.CreateShortcut($env:USERPROFILE + "\Desktop\IntelliDealer.url") $Favorite.TargetPath = "http://pfwf8033.cdkglobal-id.net:20203/intellidealer/index.html?_cb=20211101153548#/login?library=PFWF8033" $Favorite.Save() } else { Write-Host 'DIS NOT Installed' } ##################################################################### #Unpin Taskbar Store and Mail function UnPinFromTaskbar { param( [string]$appname ) Try { ((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | Where-Object { $_.Name -eq $appname }).Verbs() | Where-Object { $_.Name -like "Ta bort fr*" -OR $_.Name -like "Unpin from*" } | ForEach-Object { $_.DoIt() } } Catch { $a = "b" } } #UnPinFromTaskbar UnPinFromTaskbar "E-Post" UnPinFromTaskbar "Microsoft Store" UnPinFromTaskbar "Mail" ######################STANDARD APPS###################################### $StandardApps = @( 'Google.Chrome' 'Mozilla.Firefox' 'Oracle.JavaRuntimeEnvironment' # Java 8 JRE (was jre8) '7zip.7zip' 'VideoLAN.VLC' 'Malwarebytes.AdwCleaner' ) foreach ($App in $StandardApps) { Install-WingetApp $App } #.NET Framework 4.8 ships with Windows 10 1903+ and Windows 11. #Uncomment only if you confirm an older PC needs it. Run: winget search "NET Framework" #Install-WingetApp '' ######################SCREENCONNECT###################################### if ($InstallSC) { # This URL places the PC in the "1 - Staging" group on the itsp ScreenConnect server. $url = 'https://itsp.screenconnect.com/Bin/ScreenConnect.ClientSetup.exe?e=Access&y=Guest&c=1%20-%20Staging&c=&c=&c=&c=&c=&c=&c=' $outpath = '.\ScreenConnect.ClientSetup.exe' # Hide the progress bar. The progress bar slows downloads in Windows PowerShell 5.1. $ProgressPreference = 'SilentlyContinue' try { Invoke-WebRequest -Uri $url -OutFile $outpath -UseBasicParsing -ErrorAction Stop } catch { Write-Warning "The ScreenConnect download failed: $($_.Exception.Message)" } $ProgressPreference = 'Continue' # Check the digital signature before running the installer. if (-not (Test-Path $outpath)) { Write-Warning 'The script could not find the ScreenConnect installer. The script skipped ScreenConnect.' } elseif (($Signature = Get-AuthenticodeSignature -FilePath $outpath).Status -eq 'Valid') { Write-Host "Signed by: $($Signature.SignerCertificate.Subject)" -ForegroundColor Green # -Wait makes the script wait until the installer finishes. Start-Process -FilePath $outpath -Wait } else { Write-Warning "The ScreenConnect installer signature is $($Signature.Status). The script did NOT run it." } } else { Write-Host 'Screen Connect NOT Installed' } ######################WINDOWS UPDATE##################################### if ($RunUpdates) { $RebootNeeded = Install-WindowsUpdates if ($RebootNeeded) { Write-Host 'Windows Update needs a restart to finish.' -ForegroundColor Yellow } } else { Write-Host 'Windows Update NOT run' } ######################RESTART############################################ # The hostname change and the screen lock setting also need a restart. $RestartNow = Get-YesNo -Prompt 'Restart now to apply all changes? (Y/n) default [n]' -Default $false if ($RestartNow) { Restart-Computer -Force } else { Write-Host 'Restart the PC later to apply all changes.' -ForegroundColor Yellow }