# Cleans up default start menu and taskbar (some settings require Windows Pro)

function Install-PSModule {
  param(
    [Parameter(Position = 0, Mandatory = $true)]
    [String[]]$Modules
  )

  Write-Output "`nChecking for necessary PowerShell modules..."
  try {
    # Set PowerShell to TLS 1.2 (https://devblogs.microsoft.com/powershell/powershell-gallery-tls-support/)
    if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12' -and [Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls13') {
      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    }

    # Install NuGet package provider
    if (!(Get-PackageProvider -ListAvailable -Name 'NuGet' -ErrorAction Ignore)) {
      Write-Output 'Installing NuGet package provider...'
      Install-PackageProvider -Name 'NuGet' -MinimumVersion 2.8.5.201 -Force
    }

    # Set PSGallery to trusted repository
    Register-PSRepository -Default -InstallationPolicy 'Trusted' -ErrorAction Ignore
    if (!(Get-PSRepository -Name 'PSGallery' -ErrorAction Ignore).InstallationPolicy -eq 'Trusted') {
      Set-PSRepository -Name 'PSGallery' -InstallationPolicy 'Trusted'
    }
  
    # Install & import modules
    ForEach ($Module in $Modules) {
      if (!(Get-Module -ListAvailable -Name $Module -ErrorAction Ignore)) {
        Write-Output "`nInstalling $Module module..."
        Install-Module -Name $Module -Force
        Import-Module $Module
      }
    }

    Write-Output 'Modules installed successfully.'
  }
  catch { 
    Write-Warning 'Unable to install modules.'
    Write-Warning $_
    exit 1
  }
}

function Set-LayoutXML {
  param (
    [Parameter (Mandatory = $true)]
    [String]$XMLFile
  )
  
  $Path = Split-Path -Path $XMLFile -Parent
  if (!(Test-Path -PathType Container $Path)) {
    New-Item -ItemType Directory -Path $Path | Out-Null
  }

  $XML = [XML]@'
<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
  <LayoutOptions StartTileGroupCellWidth="6" />
  <DefaultLayoutOverride>
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="6">
        <start:Group Name="">
          <start:Tile Size="2x2" Column="2" Row="2" AppUserModelID="Microsoft.ScreenSketch_8wekyb3d8bbwe!App" />
          <start:Tile Size="2x2" Column="0" Row="2" AppUserModelID="Microsoft.Windows.Photos_8wekyb3d8bbwe!App" />
          <start:Tile Size="2x2" Column="2" Row="0" AppUserModelID="Microsoft.WindowsStore_8wekyb3d8bbwe!App" />
          <start:Tile Size="2x2" Column="4" Row="0" AppUserModelID="Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe!App" />
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk" />
          <start:Tile Size="2x2" Column="4" Row="2" AppUserModelID="MicrosoftCorporationII.QuickAssist_8wekyb3d8bbwe!App" />
        </start:Group>
        <start:Group Name="Office">
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Excel.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="4" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\PowerPoint.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Word.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="4" Row="2" DesktopApplicationLinkPath="%APPDATA%\Microsoft\Windows\Start Menu\Programs\Microsoft Teams.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="2" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Outlook.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="2" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\OneDrive.lnk" />
        </start:Group>
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
</LayoutModificationTemplate>
'@

  $XML.Save("$XMLFile")
}

# Variables
$Modules = @('PolicyFileEditor')
$ComputerPolicyFile = ($env:SystemRoot + '\System32\GroupPolicy\Machine\registry.pol')
$UserPolicyFile = ($env:SystemRoot + '\System32\GroupPolicy\User\registry.pol')
$WinVer = Get-WmiObject win32_operatingsystem
Set-Location -Path $env:SystemRoot

# Define policies
$ComputerPolicies = @(
  [PSCustomObject]@{Key = 'Software\Microsoft\Windows\CurrentVersion\Communications'; ValueName = 'ConfigureChatAutoInstall'; Data = '0'; Type = 'Dword' } # Disable Teams (personal) auto install (W11)
  [PSCustomObject]@{Key = 'Software\Policies\Microsoft\Windows\Windows Chat'; ValueName = 'ChatIcon'; Data = '2'; Type = 'Dword' } # Hide Chat icon by default (W11)
  [PSCustomObject]@{Key = 'Software\Policies\Microsoft\Windows\Windows Search'; ValueName = 'AllowCortana'; Data = '0'; Type = 'Dword' } # Disable Cortana
  [PSCustomObject]@{Key = 'Software\Policies\Microsoft\Windows\Windows Feeds'; ValueName = 'EnableFeeds'; Data = '0'; Type = 'Dword' } # Disable news/interests on taskbar
  [PSCustomObject]@{Key = 'Software\Policies\Microsoft\Windows\Windows Search'; ValueName = 'DisableWebSearch'; Data = '1'; Type = 'Dword' } # Disable web search in Start
  [PSCustomObject]@{Key = 'Software\Policies\Microsoft\Windows\Windows Search'; ValueName = 'AllowCloudSearch'; Data = '0'; Type = 'Dword' } # Disable web search in Start
  [PSCustomObject]@{Key = 'Software\Policies\Microsoft\Windows\CloudContent'; ValueName = 'DisableCloudOptimizedContent'; Data = '1'; Type = 'Dword' } # Disable cloud consumer content
  [PSCustomObject]@{Key = 'Software\Policies\Microsoft\Windows\CloudContent'; ValueName = 'DisableConsumerAccountStateContent'; Data = '1'; Type = 'Dword' } # Disable cloud consumer content
  [PSCustomObject]@{Key = 'Software\Policies\Microsoft\Windows\CloudContent'; ValueName = 'DisableWindowsConsumerFeatures'; Data = '1'; Type = 'Dword' } # Disable Consumer Experiences
)

$UserPolicies = @(
  [PSCustomObject]@{Key = 'Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced'; ValueName = 'TaskbarMn'; Data = '0'; Type = 'Dword' } # Disable Chat Icon
  [PSCustomObject]@{Key = 'Software\Microsoft\Windows\CurrentVersion\Policies\Explorer'; ValueName = 'HideSCAMeetNow'; Data = '1'; Type = 'Dword' } # Disable Meet Now icon (W10)
  [PSCustomObject]@{Key = 'Software\Microsoft\Windows\CurrentVersion\Search'; ValueName = 'SearchboxTaskbarMode'; Data = '0'; Type = 'Dword' } # Set Search in taskbar, 1 = icon only, 0 = OFF
  [PSCustomObject]@{Key = 'Software\Policies\Microsoft\Windows\CloudContent'; ValueName = 'DisableWindowsSpotlightFeatures'; Data = '1'; Type = 'Dword' } # Disable Windows Spotlight
)

# Set group policies
Install-PSModule $Modules
try {
  Write-Output 'Setting group policies...'
  $ComputerPolicies | Set-PolicyFileEntry -Path $ComputerPolicyFile -ErrorAction Stop
  $UserPolicies | Set-PolicyFileEntry -Path $UserPolicyFile -ErrorAction Stop
  gpupdate /force /wait:0 | Out-Null
  Write-Output 'Group policies set.'
}
catch {
  Write-Warning 'Unable to apply group policies.'
  Write-Output $_
}

# Cleanup start menu & taskbar
try {
  if ($WinVer.Caption -like '*Windows 11*') {
    
    
    # Reset existing start menu layouts
    $Layout = 'AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState'
    Get-ChildItem 'C:\Users' | ForEach-Object { Remove-Item "C:\Users\$($_.Name)\$Layout" -Recurse -Force -ErrorAction Ignore }

    # Remove chat & widget icons (current user)
    $Path = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced'
    $Settings = @(
      [PSCustomObject]@{ Name = 'TaskbarMn'; Value = 0; PropertyType = 'DWORD'; } # Disable Chat Icon
      [PSCustomObject]@{ Name = 'TaskbarDa'; Value = 0; PropertyType = 'DWORD'; } # Disable Widget Icon
    )
    if (!(Test-Path $Path)) { New-Item -Path $Path -Force | Out-Null }
    $Settings | New-ItemProperty -Path $Path -Force | Out-Null
  }
  elseif ($WinVer.Caption -like '*Windows 10*') {
    $LayoutModification = 'C:\Users\Default\AppData\Local\Microsoft\Windows\Shell\LayoutModification.xml'
    Set-LayoutXML -XMLFile $LayoutModification
    New-PSDrive -PSProvider 'Registry' -Name 'HKU' -Root 'HKEY_USERS' | Out-Null
    $SubKeys = Get-ChildItem -Path 'HKU:' | Where-Object { $_.PSChildName -match '^S-\d-(?:\d+-){1,14}\d+$' } | Select-Object -ExpandProperty PSChildName
    foreach ($SubKey in $SubKeys) {
      $Path = "HKU:\$SubKey\SOFTWARE\Microsoft\Windows\CurrentVersion\CloudStore\Store\Cache\DefaultAccount"
      if (Test-Path -Path $Path) { Remove-Item -Path $Path -Force -Recurse }
    }
    Remove-PSDrive -Name 'HKU'
  }

  # Restart Explorer 
  if ($env:USERNAME -ne 'defaultuser0') { Stop-Process -Name explorer -Force }
}
catch {
  Write-Warning 'Unable to complete start menu & taskbar cleanup tasks.'
  Write-Output $_
}
