CD Into New TMP Folder

I’m hard stuck trying to figure if this is possible. I need to cd into a newly created .tmp folder. The problem is that there isn’t a constant folder name for the .tmp folder. The issue in the code is at cd C:\temp\KillMcAfee\XXXXXXX.tmp. This script will download McAfee Consumer Tool Removal and run it if someone can help fix this issue. Thank you.

#Runs Powershell as Administrator and Bypass ExecutionPolicy
if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
Start-Process PowerShell -Verb RunAs -ArgumentList “-NoProfile -ExecutionPolicy Bypass -Command "cd '$pwd'; & '$PSCommandPath';”";
Exit;
}
}

#New temp folder
New-Item “C:\temp” -itemType Directory

#Download MCPR
$url = “https://download.mcafee.com/molbin/iss-loc/SupportTools/MCPR/MCPR.exe
$dest = “C:\temp\MCPR.exe”
Invoke-WebRequest -Uri $url -OutFile $dest

#Run, Copy MCPR data, Stop Process
start-process C:\temp\MCPR.exe -verb runas
cd $Env:LocalAppData\Temp
copy-item -Path “$Env:LocalAppData\Temp*” -Destination “C:\temp\KillMcAfee” -Recurse -Force -ErrorAction SilentlyContinue
start-sleep -Seconds 3
stop-process -Name “McClnUI”

#Run Mccleanup
cd C:\temp\KillMcAfee\XXXXXXX.tmp
.\Mccleanup.exe -p StopServices,MFSY,PEF,MXD,CSP,Sustainability,MOCP,MFP,APPSTATS,Auth,EMproxy,FWdiver,HW,MAS,MAT,MBK,MCPR,McProxy,McSvcHost,VUL,MHN,MNA,MOBK,MPFP,MPFPCU,MPS,SHRED,MPSCU,MQC,MQCCU,MSAD,MSHR,MSK,MSKCU,MWL,NMC,RedirSvc,VS,REMEDIATION,MSC,YAP,TRUEKEY,LAM,PCB,Symlink,SafeConnect,MGS,WMIRemover,RESIDUE -v -s

#Delete temp folder
Get-ChildItem C:\temp -Recurse | Remove-Item -Force -Recurse

#Empty RecycleBin
Clear-RecycleBin -Force

#Press Enter To Exit
Read-Host -Prompt “PRESS ENTER TO EXIT”

Mox,
Welcome to the forum. :wave:t4:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

I don’t get what your problem is. You copy the complete tmp folder AND all folders starting with the pattern “temp*” from your localAppData folder to another location. Since you copy the folder by yourself (or by your code) you can easily determine its name by using a Get-ChildItem with the same filter pattern you use for your Copy-Item!? :man_shrugging:t4:

Regardless of that - if you do all this just to be able to run the executable Mccleanup.exe, why don’t you look for this particular executable with a Get-ChildItem and use it with the path you get as result this way?

Thank you for the response. TEST TEST. I’m trying to run it as a script so that I can just walk away and do other tasks. I’m also going to add other things to the script for new pc setups at my work. So, I wouldn’t be able to look into what the folder name would be.

What would my Get-ChildItem parameters be? Could you give an example of code? It would need to find the exact name of the .tmp folder it creates when opening the exe so I could cd into it. I was looking into LastWriteTime last night.

If you use this …

… to copy one or more folders you can use this …

$FolderList = Get-ChildItem -Path "$Env:LocalAppData\Temp*"

… to have the one folder or a list of folders fitting the pattern in the variable $FolderList. :wink:

Please be aware that you will copy ALL folders starting with the string temp. Like ..\temp\ or ..\temporary internet files\ and so on. :wink:

Why do you actually copy the folder? :thinking: That seems unnecessary.

Thank you. I was thinking the same thing on why I have to copy the temp dir. I was following a guide online… But the guide said the name of the folder will be named, “MCPR”, and it’s not. The .tmp folder name will be random.

I figured it out!!!

$Now = Get-Date
Get-ChildItem C:\$Env:LocalAppData\Temp\*.tmp | Where-Object { $_.LastWriteTime -gt $Now.AddSeconds(-10) } | Rename-Item -NewName "MCPRtemp"
cd $ENv:LocalAppData\Temp\MCPRtemp

It finds the .tmp folder that was created within the last 10 seconds. :grin: