批量下载桌面壁纸使用Powershell

Posted on 2019-07-15 13:35  努力成长静待花开  阅读(478)  评论(0编辑  收藏  举报

实现效果:

  

实现代码:

function Download-Wallpaper
{
    Param
    (
        [Parameter(Mandatory=$true)]
        $Folder,

        [Parameter(ValueFromPipeline)]
        [int]
        $Page=1
    )

    Begin
    {
        $url="http://wallpaperswide.com/page/$Page"
        $targetExists=Test-Path -Path $Folder
        if(!$targetExists){New-Item -Path $Folder -ItemType Directory}
    }
    Process
    {
        $web=Invoke-WebRequest -Uri $url -UseBasicParsing    
        $web.Images.src|
        ForEach-Object{
            $filename=$_.split('/')[-1].Replace('t1.jpg','wallpaper-1366x768.jpg')
            $source="http://wallpaperswide.com/download/$filename"
            $TargetPath=Join-Path -Path $Folder -ChildPath $filename
            Invoke-WebRequest -Uri $source -OutFile $TargetPath
        }
    }
    End
    {
        explorer $Folder
    }
}