Forum Discussion
TJCooper440
Mar 09, 2022Copper Contributor
Migrating H drives to One Drive - Create csv
I am trying to create a CSV to migrate H drives to one drive. The CSV needs to have the path to the H drive and the SharePoint path to one drive. I am having trouble creating a script that can export the two strings. This is what I have.
$users = Get-ADUser -Searchbase "OU=test,DC=Whatever,DC=LOC" -filter *
Foreach ($user in $users) {
$homedirectory = (Get-Aduser -Identity $user -properties homedirectory | Select homeDirectory )
$OneDrivePath = "\\test\test\" + (Get-ADUser -Identity $user -properties SAMAccountName | Select SAMAccountname)
$output = New-Object -Typename psobject -Property @{
HomeDirectory = $homedirectory
OneDrivePath = $OneDrivePath
}
}
$output | Select HomeDirectory, OneDrivePath | Export-csv "C:\temp\test.csv"
TJCooper440 Ah, no problem. Changed it again, a bit more compact and also using only one Get-Aduser like AndySvints mentioned 🙂
$Users = Get-ADUser -Searchbase "OU=users,OU=Corp,DC=test,DC=local" -Filter * -Properties HomeDirectory, SamAccountName $TotalOutput = @() Foreach ($User in $Users) { $OneDrivePath = "\\test\test\" + $user.SamAccountName + "_constoso_loc" $Output = New-Object -Typename psobject $Output | Add-Member -MemberType NoteProperty -Name HomeDirectory -Value $User.HomeDirectory $Output | Add-Member -MemberType NoteProperty -Name OneDrivePath -Value $OneDrivePath $Totaloutput += $Output } $Totaloutput | Export-csv "C:\temp\test.csv" -NoTypeInformation
Output:"HomeDirectory","OneDrivePath" "\\w2k22dc\home$\test.user1","\\test\test\test.user1_constoso_loc" "\\w2k22dc\home$\test.user2","\\test\test\test.user2_constoso_loc" "\\w2k22dc\home$\test.user3","\\test\test\test.user3_constoso_loc"
I Changed it a little bit for the $Homedirectory and $OneDrivePath but also for the Export-Csv.. Added a $totaloutput array to store the values in.
$users = Get-ADUser -Searchbase "OU=users,OU=Corp,DC=test,DC=local" -filter * $totaloutput = @() Foreach ($user in $users) { $homedirectory = (Get-Aduser -Identity $user -properties homedirectory | Select-Object homeDirectory ).HomeDirectory $OneDrivePath = "\\test\test\" + (Get-ADUser -Identity $user -properties SAMAccountName | Select-Object SAMAccountname).SamAccountName $output = New-Object -Typename psobject $output | Add-Member -MemberType NoteProperty -Name HomeDirectory -Value $homedirectory $output | Add-Member -MemberType NoteProperty -Name OneDrivePath -Value $OneDrivePath $totaloutput += $output } $totaloutput | Select-Object HomeDirectory, OneDrivePath | Export-csv "C:\temp\test.csv" -NoTypeInformation
This gives me the following csv
"HomeDirectory","OneDrivePath"
"\\w2k22dc\home$\test.user1","\\test\test\test.user1"
"\\w2k22dc\home$\test.user2","\\test\test\test.user2"
"\\w2k22dc\home$\test.user3","\\test\test\test.user3"- TJCooper440Copper Contributor
Thank you. That worked. How would I add more to this string. I realized the doc needs _contoso_loc to the end. I tried + "_contoso_loc", but its not working
It needs to be \\test\test\(samaccountname)_constoso_loc.
Again, thank you. It helped out tremendously.$OneDrivePath = "\\test\test\" + (Get-ADUser -Identity $user -properties SAMAccountName | Select-Object SAMAccountname).SamAccountName
- Did the last adjustments got you the right output?
- AndySvintsSteel Contributor
Hello Harm_Veenstra and TJCooper440,
In addition to Harm's reply I would like to add that instead of running Get-ADUser 3 times you can run it ones(on 1st line), and get all the needed properties(HomeDirectory, SAMAccountName etc.) and then rotate them into PSObject as you needed.
Also I would add user's first and last name to output array just for future reference and convenience.
Hope that helps.
- You're right, I only fixed the output thing but yes.. More efficient like that