question

BHodgins-3046 avatar image
0 Votes"
BHodgins-3046 asked ·

how do I merge a pre-defined drive letter variable with the contects of a text file called using get-content?

Hi, I'm struggling with the correct way to concatenate a pre-defined variable with the contents of a text file pulled from GC.variable:
$DriveLetter = ($MountResult | Get-Disk | Get-Partition).DriveLetter

merged with each line contained within:
$List = (Get-Content "C:\scripts\UPD_cleanup.txt")

\appdata\local\temp*
\AppData\Roaming\Mozilla\Firefox\Crash Reports
\AppData\Local\Microsoft\WebCache.old

to end up with:
G:\appdata\local\temp*
G:\AppData\Roaming\Mozilla\Firefox\Crash Reports
G:\AppData\Local\Microsoft\WebCache.old etc.

I tried a few things including:

 $List = ($DriveLetter + ":")+(Get-Content "C:\scripts\UPD_cleanup.txt")

but it only worked for the first entry.

Thanks,

Brad






windows-server-powershell
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi @BHodgins-3046 ,

You can try this

 $List = Get-Content "C:\scripts\UPD_cleanup.txt" | ForEach-Object {
     $DriveLetter + ":" + $_
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BHodgins-3046 avatar image
0 Votes"
BHodgins-3046 answered ·

Thank you Ian. I tried you suggestion and at first it didn't work, then I realized that I was using get-content instead of get-childitem on the delete line. Once I changed that, everything started working. Not sure if it is the most efficient code or not, but at least I see results now:

                     $Logfile = "C:\scripts\removal_logfile3.txt"
                     Start-Transcript $Logfile -Append
                            
                          $List = Get-Content "C:\scripts\Profile_cleanup.txt" | ForEach-Object {
                             $DriveLetter + ":" + $_
                                  }
    
                     get-childitem -path $List | Remove-Item -Force -Recurse -Confirm:$false -ErrorAction SilentlyContinue -verbose
                     Stop-Transcript


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi @BHodgins-3046 ,

You may try robocopy. It should be much faster.

https://tylermade.net/2017/10/06/how-to-delete-all-files-in-a-directory-with-robocopy/

Tip: This answer contains the content of a third-party website. Microsoft makes no representations about the content of these websites. We provide this content only for your convenience.

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.