ProductivityTools.PSSetLockScreen.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<#
    My Function
#>

function Set-LockScreen{

    [cmdletbinding()]
    param(
        [string]$ImagePath
    )
    Write-Verbose "Hello from SetLockScreen"
    Write-Verbose "Image path provided by user: $ImagePath"
    
    if((Get-Item $ImagePath) -is [System.IO.DirectoryInfo])
    {
        throw "Only files"
    }

    if ($(Test-IfAdmin) -eq $false) {
        throw "You are not admin, but you would like to make change to regedit and System32. Please run Powershell as Administrator"
    }

    #https://docs.microsoft.com/en-us/windows/client-management/mdm/personalization-csp
    $PersonalizationCSPRegKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP"
    $LockScreenImagePath= "C:\Windows\System32\LockScreen.jpg"

    Write-Verbose "Personalization key: $PersonalizationCSPRegKeyPath"
    Write-Verbose "Windows LockScreen source image path: $LockScreenImagePath"
    
    Write-Verbose "Copy provided image to the Windows LockScreen source image path"
    Write-Verbose "$ImagePath=>$LockScreenImagePath"

    Copy-Item $ImagePath $LockScreenImagePath -Force

    if(Test-Path $PersonalizationCSPRegKeyPath) 
    {
        Write-Verbose "$PersonalizationCSPRegKeyPath already exists"
    }
    else
    {
        Write-Verbose "$PersonalizationCSPRegKeyPath doesn't exist - creating"
        New-Item -Path $PersonalizationCSPRegKeyPath -Force | Out-Null
    }

    Write-Verbose "Creating LockScreenImageStatus property in $PersonalizationCSPRegKeyPath registry key"
    New-ItemProperty -Path $PersonalizationCSPRegKeyPath -Name "LockScreenImageStatus" -Value 1 -PropertyType DWORD -Force | Out-Null
    Write-Verbose "Creating LockScreenImagePath property in $PersonalizationCSPRegKeyPath registry key"
    New-ItemProperty -Path $PersonalizationCSPRegKeyPath -Name "LockScreenImagePath" -Value $LockScreenImagePath -PropertyType STRING -Force | Out-Null
    Write-Verbose "Creating LockScreenImageUrl property in $PersonalizationCSPRegKeyPath registry key"
    New-ItemProperty -Path $PersonalizationCSPRegKeyPath -Name "LockScreenImageUrl" -Value $LockScreenImagePath -PropertyType STRING -Force | Out-Null

    Write-Verbose "Set-LockScreen bye bye!"
}