What you are trying to do is PowerShell beginner stuff.
Firstly, your search and destination are the same and that is not a thing to do. If you tired that in Windows Explorer it would yell at you saying the destination is that same. Thus that $searchfile variable is also moot.
Why are you just not using the built-in Windows robocopy.exe?
robocopy | Microsoft Docs
That is why it exists. Don't script if you don't have to, especially when the OS already provides a tool for the job. The Windows robocopy.exe is far more performant than what you are doing.
Your end-use case is to mirror the souredir to the destdir. robocopy has a mirror option.
/mir Mirrors a directory tree (equivalent to /e plus /purge). For
additional information, see Remarks.
Robocopy /MIR switch – mirroring file permissions
As far as your script is concerned, you have way over complicated this.
If all you are doing is mirroring the source to that user profile, robocopy will do this, but if you just want to use PowerShell, just because, this the process is far simpler that what you are doing.
# See what is in the Source and Destination
'D:\Source','D:\Destination' |
ForEach {
"`n********* Checking contnets of $PSItem *********`n"
Get-ChildItem -Path $PSItem -Recurse
}
<#
# Results
********* Checking contnets of D:\Source *********
Directory: D:\Source
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 14-Mar-20 17:03 here
d----- 13-Apr-20 22:40 NewFolder
-a---- 06-Feb-20 14:02 28817 23694d1213305764-revision-number-in-excel-book1.xls
-a---- 29-Dec-19 21:50 69 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
-a---- 13-Mar-20 12:19 70 abc.txt
-a---- 02-Feb-20 23:21 0 audiolengthCLEAN.csv
Directory: D:\Source\NewFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 03-Feb-20 11:55 New folder
d----- 20-Jan-20 11:17 temp
-a---- 29-Dec-19 21:50 69 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
-a---- 10-Jan-20 17:59 0 awél.txt
-a---- 04-Jan-20 02:01 39 hello.bat
-a---- 04-Jan-20 01:43 44 hello.ps1
-a---- 04-Jan-20 02:16 64 mytest - Copy.txt
-a---- 04-Jan-20 02:16 64 mytest.txt
-a---- 19-Jan-20 14:49 230 mytest.zip
-a---- 01-Jan-20 21:34 145 System Restore fails- AppxStaging %ProgramFiles%-WindowsApp 0x80070091 - Windows 10 Forums.url
-a---- 19-Jan-20 20:10 203 Test.csv
-a---- 14-Jan-20 23:03 8552 Test.xlsx
-a---- 14-Jan-20 21:11 988 Test.xlsx - Shortcut.lnk
-a---- 01-Jan-20 20:41 109 Welcome to Microsoft Edge Beta Channel.url
********* Checking contnets of D:\Destination *********
#>
# Move all things in the Source to the destination
'D:\Source\' |
ForEach {
Get-ChildItem -Path $PSItem |
Move-Item -Destination 'D:\Destination' -WhatIf
}
<#
# Results
What if: Performing the operation "Move Directory" on target "Item: D:\Source\here Destination: D:\Destination\here".
What if: Performing the operation "Move Directory" on target "Item: D:\Source\NewFolder Destination: D:\Destination\NewFolder".
What if: Performing the operation "Move File" on target "Item: D:\Source\23694d1213305764-revision-number-in-excel-book1.xls Destination: D:\Destination\23694d1213305764-revision-number-in-excel-bo
ok1.xls".
What if: Performing the operation "Move File" on target "Item: D:\Source\5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url Destination: D:\Destination\5 Free Software You'll Wish You
Knew Earlier! 2019 - YouTube.url".
What if: Performing the operation "Move File" on target "Item: D:\Source\abc.txt Destination: D:\Destination\abc.txt".
What if: Performing the operation "Move File" on target "Item: D:\Source\audiolengthCLEAN.csv Destination: D:\Destination\audiolengthCLEAN.csv".
#>
# Remove the whatIf to have the action happen
# Move all things in the Source to the destination
'D:\Source\' |
ForEach {
Get-ChildItem -Path $PSItem |
Move-Item -Destination 'D:\Destination'
}
# See what is in the Source and Destination
'D:\Source','D:\Destination' |
ForEach {
"`n********* Checking contnets of $PSItem *********`n"
Get-ChildItem -Path $PSItem -Recurse
}
<#
# Results
********* Checking contnets of D:\Source *********
********* Checking contnets of D:\Destination *********
Directory: D:\Destination
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 14-Mar-20 17:03 here
d----- 13-Apr-20 22:40 NewFolder
-a---- 06-Feb-20 14:02 28817 23694d1213305764-revision-number-in-excel-book1.xls
-a---- 29-Dec-19 21:50 69 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
-a---- 13-Mar-20 12:19 70 abc.txt
-a---- 02-Feb-20 23:21 0 audiolengthCLEAN.csv
Directory: D:\Destination\NewFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 03-Feb-20 11:55 New folder
d----- 20-Jan-20 11:17 temp
-a---- 29-Dec-19 21:50 69 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
-a---- 10-Jan-20 17:59 0 awél.txt
-a---- 04-Jan-20 02:01 39 hello.bat
-a---- 04-Jan-20 01:43 44 hello.ps1
-a---- 04-Jan-20 02:16 64 mytest - Copy.txt
-a---- 04-Jan-20 02:16 64 mytest.txt
-a---- 19-Jan-20 14:49 230 mytest.zip
-a---- 01-Jan-20 21:34 145 System Restore fails- AppxStaging %ProgramFiles%-WindowsApp 0x80070091 - Windows 10 Forums.url
-a---- 19-Jan-20 20:10 203 Test.csv
-a---- 14-Jan-20 23:03 8552 Test.xlsx
-a---- 14-Jan-20 21:11 988 Test.xlsx - Shortcut.lnk
-a---- 01-Jan-20 20:41 109 Welcome to Microsoft Edge Beta Channel.url
#>