YokoRUMsetPC.psm1

function Test-Admin
{
    [CmdletBinding()]
    [OutputType([boolean])]
    param ()
    
    return [boolean]([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

function Format-DateTime
{
    [CmdletBinding()]
    param ()
    
    #region Parameters
    $DateFormat = 'dd.MM.yyyy'
    $DateLongFormat = 'dddd, MMMM d, yyyy'
    $TimeZoneAuto = 'On'
    $TimeZone = 'Central Europe Standard Time'
    $TimeFormat = 'HH:mm'
    $TimeLongFormat = 'HH:mm:ss'
    #endregion
    #region DateTimeFormat
    if (!(Test-Admin -WarningAction SilentlyContinue))
    {
        Write-Warning -Message "To set time zone to automatically, need to open admin console."
    }
    else
    {
        Write-Output -InputObject "Set time zone automatically:`t$TimeZoneAuto"
        $TZAutoSettingRegPath = "HKLM:\SYSTEM\CurrentControlSet\Services\tzautoupdate"
        Set-ItemProperty -Path $TZAutoSettingRegPath -Name "Start" -Value 3
        Set-TimeZone -Id $TimeZone
    }

    Write-Output -InputObject "Setting location to:`t`tSerbia"
    Write-Output -InputObject "Setting Time Zone to:`t`t$TimeZone"
    Write-Output -InputObject "Setting short time format to:`t$TimeFormat"
    Write-Output -InputObject "Setting short date format to:`t$DateFormat"
    Write-Output -InputObject "Setting long date format to:`t$dateLongFormat"
    Write-Output -InputObject "Setting long time format tp:`t$TimeLongFormat"
    Set-WinHomeLocation -GeoId 271
    try
    {
        $status = New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS -ErrorAction SilentlyContinue
        
        if (!$status) { Write-Warning -Message "Date and time format already set." }
        
        function Set-DateFormat
        {
            [CmdletBinding(SupportsShouldProcess = $true)]
            param ($sid)
            
            if (Test-Path "HKU:\${sid}")
            {
                Write-Output -InputObject "Updating date format for user:`t$sid"
                Set-ItemProperty -Path "HKU:\${sid}\Control Panel\International\" -name sShortDate -value $DateFormat
                Set-ItemProperty -Path "HKU:\${sid}\Control Panel\International\" -name sLongDate -value $DateLongFormat
                Set-ItemProperty -Path "HKC:\Control Panel\International" -name sShortTime -value $TimeFormat
                Set-ItemProperty -Path "HKC:\Control Panel\International" -name sTimeFormat -value $TimeLongFormat
            }
        }
        
        $userDetails = Get-CimInstance -Class win32_useraccount | Where-Object{ $_.status -eq 'ok' } # Get-WmiObject
        foreach ($user in $userDetails)
        {
            $sid = $user.SID
            Set-DateFormat($sid)
        }
    }
    catch
    {
        Write-Error -Message "Error occured while setting Date format." -Exception "$_.Exception.Message" -Category WriteError
    }
    #endregion
}

function Install-dotNET3.5
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        [switch]$Force
    )
    
    if (!(Test-Admin -ErrorAction SilentlyContinue))
    {
        return Write-Error -Message "Need to open Powershell with Admin privileges." -Category PermissionDenied
    }
    
    if ($pscmdlet.ShouldProcess("Target", "Operation"))
    {
        $dotNetName = "Windows feature .NET 3.5"
        $dotNet = (Get-WindowsOptionalFeature -FeatureName "NetFx3" -Online -ErrorAction SilentlyContinue).State
        
        if ($Force)
        {
            Write-Output "Installing optional $dotNetName package."
            Enable-WindowsOptionalFeature -Online -FeatureName "NetFx3" -All
        }
        elseif (![boolean]$dotNet)
        {
            Write-Output "Installing optional $dotNetName package."
            Enable-WindowsOptionalFeature -Online -FeatureName "NetFx3" -All
        }
        else
        { Write-Output "Optional $dotNetName is already enabled." }
        
        if (![boolean]$dotNet)
        {
            Write-Error -Category NotEnabled -Message "$dotNetName failed to install, trying other installation method." -ErrorAction Continue
            $choco = Get-Command -Name "choco.exe" -ErrorAction SilentlyContinue
            
            if (![boolean]$choco)
            {
                [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
                return & ([ScriptBlock]::Create((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')))
            }
            else
            { Start-Process -FilePath "choco.exe" -ArgumentList "install dotnet3.5 -f -y" -NoNewWindow }
            
            if (![boolean]$choco)
            { Start-Process -FilePath "DISM" -ArgumentList "/Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:D:\sources\sxs" -NoNewWindow }
        }
    }
}

function Set-Other
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param ()
    
    function Show-TrayIcon
    {
        [CmdletBinding()]
        param ()
        
        $Key = 'Registry::HKCU\Control Panel\NotifyIconSettings'
        $Property = 'IsPromoted'
        
        if (Test-Admin)
        {
            Write-Output "Showing all tray icons."
            if (Test-Path -LiteralPath $Key -PathType 'Container')
            {
                Get-ChildItem -LiteralPath $Key |
                Where-Object { $_.GetValue($Property) -ne 1 } |
                ForEach-Object {
                    Set-ItemProperty -LiteralPath ($_.PSPath) -Name $Property -Value 1 -Force
                    Write-Host "Set IsPromoted to 1 for $($_.PSPath)"
                }
            }
            else { return Write-Output "Registry path '$Key' not found." }
        }
        else { return Write-Warning -Message "To show all tray icons need admin console." }
    }
    
    if ($pscmdlet.ShouldProcess("Target", "Operation"))
    {
        if (Test-Admin)
        {
            Write-Output -InputObject "File delete confirmation:`tOn"
            $RegCheck = Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer" -ErrorAction SilentlyContinue
            if (!$RegCheck) { New-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer\" -Force | Out-Null }
            New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer\" -Name "ConfirmFileDelete" -Value 1 -Force -ErrorAction SilentlyContinue
        }
        else { Write-Warning -Message "To set file delete confirmation dialog, need to open admin console." }
        $WinVer = Get-ComputerInfo | Select-Object OsName, OsDisplayVersion, OsBuildNumber
        if ((Test-Admin) -and ($WinVer.OsName -like "*11*"))
        {
            Write-Output -InputObject "Moving taslbar buttons to:`tLeft"
            New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "TaskbarAl" -Value 0 -PropertyType DWORD -Force -ErrorAction SilentlyContinue
        }
        elseif ($WinVer.OsName -like "*10*") { Write-Output -InputObject "Windows 10 detected, tasbar already on the left side." }
        else { Write-Warning -Message "To move taskbar to left, need to open admin console." }
    }
    
    if (Test-Admin)
    {
        Write-Output -InputObject "Enabling My Computer, User's files and Network icnos on Desktop."
        $desktopIconSettingsPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel'
        $desktopSettings = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\"
        $ThemesChange = "ThemeChangesDesktopIcons"
        $MyComputer = "{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
        $UserFiles = "{59031a47-3f72-44a7-89c5-5595fe6b30ee}"
        $Network = "{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}"
        New-ItemProperty -Path $desktopIconSettingsPath -Name $MyComputer -Value 0 -Force
        New-ItemProperty -Path $desktopIconSettingsPath -Name $UserFiles -Value 0 -Force
        New-ItemProperty -Path $desktopIconSettingsPath -Name $Network -Value 0 -Force
        New-ItemProperty -Path $desktopSettings -Name $ThemesChange -Value 0 -Force
    }
    else { Write-Warning -Message "Cannot setup Desktop icons without admin console." }
    
    Show-TrayIcon
}

<#
    .SYNOPSIS
        Make HWID CSV file
     
    .DESCRIPTION
        Gets HWID PC information and makes 'AutopilotHWID.csv' file into folder HWID at system root drive.
     
    .EXAMPLE
                PS C:\> Get-HWIDcsv
     
    .NOTES
        No additional info, this is for private use.
#>

function Get-HWIDcsv
{
    [CmdletBinding()]
    param ()
    
    if (Test-Admin)
    {
        Write-Output -InputObject "Generating hardware hash file."
        Install-Script -Name Get-WindowsAutoPilotInfo -Force
        $hashFolder = $env:SystemDrive + "\HWID"
        New-Item -Path $hashFolder -ItemType Directory -Force | Out-Null
        Set-Location -Path $hashFolder
        Get-WindowsAutopilotInfo.ps1 -OutputFile "AutopilotHWID.csv"
        Start-Process -FilePath explorer $hashFolder
    }
    else { Write-Error -Message "Cannot generate hardware hash file, need admin console." -Category AuthenticationError }
}

<#
    .SYNOPSIS
        Set user PC
     
    .DESCRIPTION
        Private cmdlet for setting Yokohama - Serbia - Ruma user PC.
     
    .EXAMPLE
                PS C:\> Set-YokoPC
     
    .NOTES
        Private cmdlet, no additional info.
#>

function Set-YokoPC
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param ()
    
    $job1 = Install-Language -Language "sr-Latn-RS" -AsJob
    Write-Output -InputObject "Intalling Serbian language packs in background.`n"
    $job2 = Install-Language -Language "sr-Cyrls-RS" -AsJob
    
    Write-Output -InputObject "Setting region format to:`tEnglish (United States)"
    Set-Culture -CultureInfo en-US
    
    Format-DateTime
    
    Write-Output -InputObject "Install M3 if needed, or open from Edge: 'https://Twsm3isoprd.yokohama-tws.com:20108/mango'"
    Start-Process -FilePath explorer -ArgumentList "P:\IT\Programi\M3"
    Start-Process -FilePath "https://Twsm3isoprd.yokohama-tws.com:20108/mango"
    Write-Output -InputObject "Set 2 Factor authorisation if needed."
    Start-Process -FilePath "https://aka.ms/mfasetup"
    
    if (Test-Admin)
    {
        Write-Output -InputObject "Removing widgets."
        Get-AppxPackage *WebExperience* | Remove-AppxPackage
    }
    else { Write-Warning -Message "Cannot remove widgets. Need admin console." }
    
    Set-Other
    
    if (Test-Admin)
    {
        Write-Output -InputObject "Disabling task view."
        Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "ShowTaskViewButton" -Value 0 -Force
        Stop-Process -Name explorer -Force
        Start-Process -PSPath explorer
    }
    else { Write-Warning -Message "Cannot disable task view. Need admin console." }
    
    Install-dotNET3.5
    
    if (($job1.State -eq "Running") -or ($job2.State -eq "Running"))
    {
        Write-Output -InputObject "Waiting for Serbian language packs to install."
        Wait-Job -Job $job1, $job2
    }
    
    Write-Output -InputObject "PC setup done, restart needed."
}