Folderredirections to log

While migrating users from a fileserver with personal mapped home directories to OneDrive I had to change the GPO`s that configured folderredirections. Before each user had their Documents and Desktop redirected to the mapped home directory. Whilst having many different GPO`s, and we rolled this out to small groups at a time. I wanted a way to monitor what folders each users was redirected to.

With  [Environment]::GetFolderPath(“MyDocuments”)  you can see the path for each of the KnownFolders (Desktop, Pictures and Documents”

So with these three lines you have the folders, ready to be stored to a file.

PowerShell
$docpath = [Environment]::GetFolderPath("MyDocuments")
$dtpath = [Environment]::GetFolderPath("Desktop")
$picpath = [Environment]::GetFolderPath("MyPictures")

As I do with most of my scripts I log the content to a file. In this specific case Im writing to a separate file for each user.

I use this function to write to a log file

PowerShell
 Function LogWrite
{
   Param ([string]$logstring)
   $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
   $logstring = $stamp +": "+ $logstring
   Add-content $Logfile -value $logstring
} 

The complete script looks like this

PowerShell
$logbasepath = "\\fileserver\logs$\onedrive\"
$currentUsername = $env:USERNAME
$logpath = $logbasepath+$currentUsername
$logfile = $logpath+"\"+$Currentusername+"-knownfolder.txt"
new-item $logbasepath -name $Currentusername -ItemType 'directory'

#My trusty simple log function, with timestamping.
Function LogWrite
{
   Param ([string]$logstring)
   $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
   $logstring = $stamp +": "+ $logstring
   Add-content $Logfile -value $logstring
}

logwrite "Script started on server $env:computername "

# Getting users folder-redirections
$docpath = [Environment]::GetFolderPath("MyDocuments")
$dtpath = [Environment]::GetFolderPath("Desktop")
$picpath = [Environment]::GetFolderPath("MyPictures")

logwrite "##########"
Logwrite "Documents: $docpath"
Logwrite "Desktop: $dtpath"
Logwrite "Pictures: $picpath"
logwrite "##########"