Scripts/Repair-FolderRedirection.ps1

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#requires -Version 3.0
function Repair-FolderRedirection
{
  <#PSScriptInfo
 
      .VERSION 1.3
 
      .GUID cc7ee278-5103-4711-909c-315e3915ba92
 
      .AUTHOR Erik
 
      .COMPANYNAME Knarr Studio
 
      .COPYRIGHT
 
      .TAGS Folder Redirecton Self Help
 
      .LICENSEURI
 
      .PROJECTURI https://github.com/KnarrStudio/ITPS.OMCS.Tools
 
      .ICONURI
 
      .EXTERNALMODULEDEPENDENCIES
 
      .REQUIREDSCRIPTS
 
      .EXTERNALSCRIPTDEPENDENCIES
 
      .RELEASENOTES
 
 
      .PRIVATEDATA
 
  #>

  <#
      .SYNOPSIS
      Verify and repair (redirect) the user's folder redirection.
 
      .DESCRIPTION
      From a normal Powershell console.
      The script will verify that the path exists, and copies all of the local files to the "Remote" location, then changes the registry to mach that remote location.
      Changes the folder redirection settings in the registry.
      This should be run prior to imaging a user's workstaion.
 
      .PARAMETER RemotePath
      Makes changes to the home folders based on what you put here. Such as - "$env:HOMEDRIVE\_MyComputer".
 
      .PARAMETER Repair
      Initiats the changes
 
      .PARAMETER Silent
      Suppresses output to console
 
      .EXAMPLE
      Repair-FolderRedirection -RemotePath 'H:\_MyComputer' -Repair
      This will redirect the folders to the path on the "H:" drive. You must use the 'Repair' parameter if you want to make changes.
 
      .EXAMPLE
      Repair-FolderRedirection
      Sends the current settings to the screen
 
      .NOTES
      Really written to standardize the troubleshooting and repair of systems before they are imaged to prevent data loss.
 
      .LINK
      https://github.com/KnarrStudio/ITPS.OMCS.Tools
 
      .INPUTS
      Remote path as a string
 
      .OUTPUTS
      Display to console.
  #>


  [CmdletBinding(SupportsShouldProcess,ConfirmImpact = 'High')]
  [OutputType([int])]
  Param
  (
    # $RemotePath Path to the Users's 'H:' drive
    [Parameter(ParameterSetName = 'Repair',ValueFromPipelineByPropertyName,Position = 0)]
    [string]$RemotePath = "$env:HOMEDRIVE\_MyComputer",
    # Use the Repair switch make changes to settings
    [Parameter(ParameterSetName = 'Repair')]
    [Switch]$Repair,
    [Switch]$Silent
  )
 
  Begin
  {
    $ConfirmPreference = 'High'
   
    $CompareList = @()

    $FolderList = @{
      'Desktop'   = 'Desktop'
      'Favorites' = 'Favorites'
      'My Music'  = 'Music'
      'My Pictures' = 'Pictures'
      'My Video'  = 'Videos'
      'Personal'  = 'Documents'
    }
    
    $Keys = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders', 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
    $LocalPath = $Env:USERPROFILE
    $errorlog = "ErrorLog-FolderRedirection-$(Get-Date -UFormat %d%S).txt"
  }
  Process
  {
    foreach($FolderKey in $FolderList.keys)
    {
      $FolderName = $FolderList.Item($FolderKey)
      $OldPath = ('{0}\{1}' -f $LocalPath, $FolderName)
      $NewPath = ('{0}\{1}' -f $RemotePath, $FolderName)
      Write-Verbose -Message ('FolderName = {0}' -f $FolderName)
      Write-Verbose -Message ('OldPath = {0}' -f $OldPath)
      Write-Verbose -Message ('NewPath = {0}' -f $NewPath)

      If(-Not(Test-Path -Path $NewPath ))
      {
        Write-Verbose -Message ('NewPath = {0}' -f $NewPath)
        if($Repair)
        {
          New-Item -Path $NewPath -ItemType Directory
        }
      }

      Write-Verbose -Message ('OldPath = {0}' -f $OldPath)
      try
      {
        if($Repair)
        {
          Copy-Item -Path $OldPath -Destination $RemotePath -Force -Recurse -ErrorAction Stop
        }
      }
      catch
      {
        $OldPath + $_.Exception.Message | Out-File -FilePath ('{0}\{1}' -f $RemotePath, $errorlog) -Append
      }
      
      foreach($RegKey in $Keys)
      {
        Write-Verbose -Message ('FolderKey = {0}' -f $FolderKey)
        Write-Verbose -Message ('FolderName = {0}' -f $FolderName)
        Write-Verbose -Message ('RegKey = {0}' -f $RegKey)
        

        $LeafKey = Split-Path -Path $RegKey -Leaf
        $CurrentSettings = Get-ItemProperty -Path $RegKey -Name $FolderKey
        $newlist = ('{2}: {0} = {1}' -f $FolderKey, $CurrentSettings.$FolderKey, $LeafKey)
        Write-Verbose -Message $newlist
        $CompareList += $newlist
       
        <# F8 Testing --
            $Key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
            Get-ItemProperty -Path $ke
        #>


        if($Repair)
        {
          Set-ItemProperty -Path $RegKey -Name $FolderKey -Value $NewPath
        }
      }
    }

  }

  END {
    if(-not $Silent)
    {
      $CompareList | Sort-Object
      Write-Output -InputObject 'Log File: ', $env:TEMP\FolderRedirection.log""
    }
    $CompareList |
    Sort-Object |
    Out-File -FilePath "$env:TEMP\FolderRedirection.log"
  }
}


# Testing:
# Repair-FolderRedirection -Silent
# Repair-FolderRedirection -Repair -RemotePath h:\_MyComputer -Confirm






# SIG # Begin signature block
# MIID/AYJKoZIhvcNAQcCoIID7TCCA+kCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU/T9SUBgOtbwsBe7QeRi+r3xO
# F++gggIRMIICDTCCAXagA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