Skip to content

Instantly share code, notes, and snippets.

@Hexalon
Last active May 15, 2016 20:57
Show Gist options
  • Save Hexalon/9e16547b66ccacb6d9c5e2772532eb92 to your computer and use it in GitHub Desktop.
Save Hexalon/9e16547b66ccacb6d9c5e2772532eb92 to your computer and use it in GitHub Desktop.
Changes default Windows 10 background images to custom background images
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.120
Created on: 4/28/2016 08:18
Created by: Colin Squier <hexalon@gmail.com>
Filename: Set-Win10-CustomBackground.ps1
===========================================================================
.DESCRIPTION
Changes default Windows 10 background images to custom background images.
#>
[CmdletBinding()]
Param ()
#Check for elevated execution
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$Arguments = "& '" + $MyInvocation.MyCommand.Definition + "'"
Start-Process powershell -Verb RunAs -ArgumentList $Arguments
Break
}
<#
.SYNOPSIS
Copies any file or files provided as input.
.DESCRIPTION
Copies any file or files provided as input, using
absolute file pathes to minimize file path errors.
.PARAMETER FilesToCopy
An array of files to copy.
.PARAMETER Destination
File path of where to files are being copied to.
.PARAMETER SourcePath
File path of where the files are being copied from.
.EXAMPLE
PS C:\> CopyFile -FilesToCopy $value1 -Destination $value2 -SourcePath $value3
.NOTES
Compatible with PS 2+ and tested on Windows 7/10.
#>
function CopyFile($FilesToCopy, $Destination, $SourcePath)
{
foreach ($File in $FilesToCopy)
{
$Path = Join-Path -Path $SourcePath -ChildPath $File
Write-Verbose "Copying $File to $Destination"
Copy-Item -Path $Path -Destination $Destination -Force
}
}
<#
.SYNOPSIS
Returns the path of the executing script's directory.
.DESCRIPTION
Sapien's implementation of the variable $HostInvocation
causes a conflict the with the system's variable.
.EXAMPLE
PS C:\> Get-ScriptDirectory
.NOTES
Work around for handling Sapien's custom host environment.
#>
function Get-ScriptDirectory
{
if ($HostInvocation -ne $null)
{
Split-Path $HostInvocation.MyCommand.path
}
else
{
Split-Path $script:MyInvocation.MyCommand.Path
}
}
$Source = Get-ScriptDirectory
#Copy background images
$FilesToCopy = "img100.jpg", "img101.png", "img102.jpg", "img103.png", "img104.jpg", "img105.jpg", "lockscreen.jpg"
$Destination = "$env:SystemRoot\Web\Screen"
Write-Verbose -Message "Taking ownership of $Destination"
$ACL = Get-ACL $Destination
$Group = New-Object System.Security.Principal.NTAccount("Builtin", "Administrators")
$ACL.SetOwner($Group)
Set-Acl -Path $Destination -AclObject $ACL
Write-Verbose -Message "Changing permissions on $Destination folder"
$Permission = $Group, "FullControl", "Allow"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $Permission
$ACL.SetAccessRule($AccessRule)
Set-Acl -Path $Destination -AclObject $ACL
Write-Verbose -Message "Enabling permission inheritance on $Destination folder"
$Permission = $Group, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $Permission
$ACL.SetAccessRule($AccessRule)
Set-Acl -Path $Destination -AclObject $ACL
$Files = (Get-ChildItem $Destination)
foreach ($File in $Files)
{
$Permission = $Group, "FullControl", "Allow"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $Permission
$ACL.SetAccessRule($AccessRule)
$ACLFile = (Join-Path -Path $Destination -ChildPath $File)
Write-Verbose -Message "Changing permissions on $ACLFile"
Set-Acl -Path $ACLFile -AclObject $ACL
}
CopyFile -FilesToCopy $FilesToCopy -Destination $Destination -SourcePath $Source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment