0

I'm working on a powershell script that set a new wallpaper on Windows 10, my problem is that I need to have the file I want to set in C:/ but for this case I need to change the path to another path in the web. Is this possible? How can I do it? Here is the code I have:

# Set Variables
$SCRIPTDIRECTORY = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$PATHWEB = "C:\Windows\WEB\Wallpaper\Windows"
$PATH4K = "C:\Windows\Web\4K\Wallpaper\Windows"
$REPLACEINHERIT = "/inheritance:r" #Remove ALL inherited Access Control
$GRANT = "/GRANT *S-1-1-0" #Grant access rights. This Syntax grants FULL control to Everyone
$USERACCOUNT = "System"
$PERMISSION = ":(OI)(CI)F" #(OI) Object Inherit; (CI) Container Inherit; (F) Full Access

# Run TAKEOWN using Invoke Expression
Invoke-Expression -Command ('TAKEOWN /f $PATHWEB /a /r')
Invoke-Expression -Command ('TAKEOWN /f $PATH4K /a /r')

# Run ICACLS using Invoke Expression
Invoke-Expression -Command ('ICACLS $PATHWEB /GRANT *S-1-1-0:F /T "${$USERACCOUNT}${$PERMISSION}"')
Invoke-Expression -Command ('ICACLS $PATH4K /GRANT *S-1-1-0:F /T "${$USERACCOUNT}${$PERMISSION}"')

# Rename Default Web Wallpaper
Remove-Item -Path "$env:windir\Web\Wallpaper\Windows\img1.jpg" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "%AppData%\roaming\Microsoft\Windows\Themes\cachedfiles\*.*" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "%AppData%\roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg" -Force -ErrorAction SilentlyContinue

# Copy New Web Wallpaper
Copy-Item "$SCRIPTDIRECTORY\img0.jpg" "$env:windir\Web\Wallpaper\Windows" -Force

# Define image file list
$IMAGES = @(
    'img0_1024x768.jpg'
    'img0_1200x1920.jpg'
    'img0_1366x768.jpg'
    'img0_1600x2560.jpg'
    'img0_2160x3840.jpg'
    'img0_2560x1600.jpg'
    'img0_3840x2160.jpg'
    'img0_768x1024.jpg'
    'img0_768x1366.jpg'
)

# Copy each 4K Image
ForEach ($IMAGE in $IMAGES)
{
    Copy-Item "$SCRIPTDIRECTORY\$IMAGE" $env:windir\Web\4K\Wallpaper\Windows -Force
}

# Make Necessary Changes To The Registry
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name Wallpaper -Value "$env:windir\Web\Wallpaper\Windows\img0.jpg" -Force | Out-Null
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperOriginX -Value "0" -Force | Out-Null
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperOriginY -Value "0" -Force | Out-Null
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name TileWallpaper -Value "0" -Force | Out-Null
Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperStyle -Value "2" -Force | Out-Null

# Run ICACLS using Invoke Expression
Invoke-Expression -Command ('ICACLS $PATHWEB /setowner "NT SERVICE\TrustedInstaller"')
Invoke-Expression -Command ('ICACLS $PATH4K /setowner "NT SERVICE\TrustedInstaller"')
Invoke-Expression -Command ('ICACLS $PATHWEB /remove Everyone /T /C')
Invoke-Expression -Command ('ICACLS $PATH4K /remove Everyone /T /C')
Kent
  • 189,393
  • 32
  • 233
  • 301
  • As an aside: [`Invoke-Expression` should generally be avoided](https://blogs.msdn.microsoft.com/powershell/2011/06/03/invoke-expression-considered-harmful/); definitely [don't use it to invoke an external program](https://stackoverflow.com/a/57966347/45375). – mklement0 May 14 '20 at 21:59
  • You cannot use `%AppData%` to reference environment variables in PowerShell; use `$env:AppData` instead. – mklement0 May 14 '20 at 22:01
  • You can use [`Invoke-WebRequest`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest) with `-OutFile` to download a file from the web. – mklement0 May 14 '20 at 22:18
  • know i did change my wallpaper getting an image from the web, BUT now I need to put it to all my users in that pc, anyone knows hoy can I do that? – Miguel Buenrostro May 18 '20 at 22:30
  • Given that your question has received no answers yet, it's acceptable to revise it. Therefore, please edit your question to formulate the problem you are facing now - please also edit the question's title accordingly. – mklement0 May 18 '20 at 22:43
  • Perhaps the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) help topic is useful too. – mklement0 May 18 '20 at 22:44

0 Answers0