title: Powershell force Onedrive sync at Logout
body: <p>I'm attempting to enforce, at logout for each user, a method to dehydrate Onedrive and synced sharepoint sites via Onedrive.  I effectively got the file flags to change with a user logoff script via our Domain controller. The file flags change, but the files will not dehydrate until the next login.  I would like to keep the VHDX profiles small by dehydrating at logoff.  I've attempted to give it a start-sleep for 30 and I've also tried starting Onedrive in the script, but I could use some help with the last phase of the script to determine how to initialize the sync before the logoff process is complete.</p>
  <pre><code>$FilesAndFolders = .{
  
      get-childitem $env:userprofile\&lt;COMPANY&gt; -Force -File -Recurse | where Attributes -eq 'Archive, ReparsePoint'
  
      get-childitem $env:userprofile\&lt;COMPANY&gt; -Force -Directory -Recurse | where Attributes -eq 'Directory, Archive, ReparsePoint'
  
  }
  
  
  $FilesAndFolders | foreach {attrib.exe $_.fullname +U -P /s /D}
  
  
  #List all Files locally synced and For each file change the attributes to U (Unpinned) and removed P (Pinned attribute)
  get-childitem $env:OneDriveCommercial -Force -File -Recurse | where Attributes -eq 'Archive, ReparsePoint' | foreach {attrib.exe $_.fullname +U -P /s}
  
  
  start $env:userprofile\AppData\Local\Microsoft\OneDrive\OneDrive.exe
  
  Start-Sleep -Seconds 30
  
  kill -name Onedrive -force
  
  exit
  </code></pre>