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

B Hodgins 1 Reputation point
2021-03-16T05:03:51.047+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-03-16T07:26:50.783+00:00

    Hi @B Hodgins ,

    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.

    0 comments No comments

  2. B Hodgins 1 Reputation point
    2021-03-17T04:13:51.057+00:00

    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
    
    0 comments No comments

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-03-17T08:28:10.703+00:00

    Hi @B Hodgins ,

    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.

    0 comments No comments