将我的文档文件夹重定向到Onedrive

时间:2017-06-26 20:08:10

标签: powershell

我需要有一个PS脚本来将用户的文档重定向到他们的Onedrive。 我目前有一个批处理文件,它将在OneDrive中创建一个目录,但我需要它来实际重定向该位置,所以没有2个我的文档。并且更喜欢使用PS。 这是我尝试的PS脚本..

$answer = Show-MsgBox -title "Changing the User Library Paths" -prompt "Do you want to change the user documents folder?" -BoxType YesNo -Icon Question
if [$answer -eq "Yes"]
    {
    $MyDirs = Get-ChildItem "$HOME" -Name -Exclude "Links", "Searches"
    if [$MyDirs]
        { 
        $ShellFolder = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
        $UserShellFolder = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
        foreach [$MyDir in $MyDirs]
            {
            $RegName = Get-RegistryKeyPropertiesAndValues $ShellFolder |Where-Object {$_.Value -match "$MyDir"} |foreach {$_.property}
            $answer = Show-MsgBox -title "Change $MyDir Folder" -prompt "Do you want to change the $Mydir folder?" -BoxType YesNo -Icon Question
            if [$answer -eq "Yes"] 
                {
                $inputbox = $null
                $inputbox = Show-Inputbox -message "Enter the path of the $MyDir folder" -title "Entering path..." -default "D:\Users\$env:username\$MyDir"
                if [$inputbox] 
                    {
                    if [![Test-Path "D:\Users\$env:username\$MyDir"]] {New-Item "D:\Users\$env:username\$MyDir" -ItemType Directory -Force}
                    Set-ItemProperty -Path $ShellFolder -Name $RegName -Value $inputbox
                    Set-ItemProperty -Path $UserShellFolder -Name $RegName -Value $inputbox
                    Move-Item "$HOME\$MyDir\*" "D:\Users\$env:username\$MyDir" -Force
                    attrib +r "D:\Users\$env:username\$MyDir"
                    rd $HOME\$MyDir -recurse -Force
                    }
                }
            }
        Show-MsgBox -title "Press OK to continue" -prompt "Folder redirection done!" -BoxType OkOnly -Icon Exclamation
        cls
        }

1 个答案:

答案 0 :(得分:0)

您可以尝试此操作来更改文档位置 和编辑多个文件夹

Add-Type -AssemblyName System.Windows.Forms    
Add-Type -AssemblyName Microsoft.VisualBasic

$answer =[System.Windows.Forms.MessageBox]::Show("Changing the User Library Paths","Do you want to change the user documents folder?" ,"YesNo","Question")
if ($answer -eq "Yes")
    {
    #read your registery location documents C:\user\someone\documents
    $RegPersonal = Get-ItemProperty -Path "HKCU:Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -name "Personal" -ErrorAction SilentlyContinue
    $RegPersonal.personal = $RegPersonal.personal.trim().ToLower()

    #ansver your Documents Current Location 
    $answer =[System.Windows.Forms.MessageBox]::Show("Current Location is  "+$RegPersonal.personal ,"Do you want to change the user documents folder?" ,"YesNo","Question")
     }

   #input new directory
    if ($answer -eq "Yes") 

        {
            $inputbox = $null  
            $inputbox = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the new path " +$RegPersonal.personal+ "folder", "Entering path...  C:\Users\"+$env:username+"\"+$RegPersonal) 
         }
             if ($answer -eq "No"){
             [void][System.Windows.Forms.MessageBox]::Show("Canceled by the user")     
             exit
             }

        #change new directory location register
        if ($inputbox) 
            {
             Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' -name Personal "$inputbox"

        }               

    [void][System.Windows.Forms.MessageBox]::Show("Press OK to continue You need restart your Pc","Folder redirection done!",[Windows.Forms.MessageBoxButtons]::"OK",[Windows.Forms.MessageBoxIcon]::"Exclamation")