# CD Into New TMP Folder

**URL:** <https://forums.powershell.org/t/cd-into-new-tmp-folder/19887>\
**Category:** PowerShell Help\
**Created:** [July 14, 2022, 7:29am UTC](https://forums.powershell.org/t/cd-into-new-tmp-folder/19887 "2022-07-14T07:29:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moxadonis](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Moxadonis](https://forums.powershell.org/u/Moxadonis)\
**Post date:** [July 14, 2022, 7:29am UTC](https://forums.powershell.org/t/cd-into-new-tmp-folder/19887/1 "2022-07-14T07:29:42Z")

</div>

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](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”

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 14, 2022, 8:00am UTC](https://forums.powershell.org/t/cd-into-new-tmp-folder/19887/2 "2022-07-14T08:00:48Z")

</div>

Mox,  
Welcome to the forum. 👋🏽

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

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`!? 🤷🏽‍♂️

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?

---

<div class="post-metadata">

**Author:** ![Moxadonis](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Moxadonis](https://forums.powershell.org/u/Moxadonis)\
**Post date:** [July 14, 2022, 3:16pm UTC](https://forums.powershell.org/t/cd-into-new-tmp-folder/19887/3 "2022-07-14T15:16:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 14, 2022, 5:54pm UTC](https://forums.powershell.org/t/cd-into-new-tmp-folder/19887/4 "2022-07-14T17:54:43Z")

</div>

If you use this …

> [@Moxadonis](#):
>
> ```auto
> copy-item -Path "$Env:LocalAppData\Temp*" -Destination 'C:\temp\KillMcAfee' -Recurse -Force -ErrorAction SilentlyContinue
> 
> ```

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

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

```

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

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

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

---

<div class="post-metadata">

**Author:** ![Moxadonis](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Moxadonis](https://forums.powershell.org/u/Moxadonis)\
**Post date:** [July 15, 2022, 1:06am UTC](https://forums.powershell.org/t/cd-into-new-tmp-folder/19887/5 "2022-07-15T01:06:05Z")

</div>

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!!!

```auto
$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. 😁

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/cd-into-new-tmp-folder/19887/6 "2024-05-16T20:17:43Z")

</div>


