Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:00:53+00:00 2026-06-08T07:00:53+00:00

I have a script that I’m working on that is intended to remove the

  • 0

I have a script that I’m working on that is intended to remove the temp folder (in C Disk), delete everything in temp, and then create a new temp folder. I have the script already created to delete everything, however I’m unsure of how you go about creating a new temp folder with Powershell. Was wondering if anyone might know how to create the new temp folder in Powershell.

#remove all temporary files
if($serverVersion.name -like "*2003*"){
    $dir = "\\$server" + '\C$\temp'
}
elseif($serverVersion.name -like "*2008*"){
    $dir = "\\$server" + '\C$\Windows\Temp'
    }

$tempArray = @()
foreach ($item in get-childitem -path $dir){
    if ($item.name -like "*.tmp"){
        $tempArray = $tempArray + $item
        }
    }

for ($i = 0; $i -le $tempArray.length; $i++){
    $removal = $dir + "\" + $tempArray[$i]
    remove-item $removal -force -recurse 
    }

Am I correctly deleting the temp folder as well and, what would I have to do to create a new temp folder?

EDIT: I’ve updated my code based on what people have suggested, was wondering if this would have the desired effects and if there’s a way to cut this down even further:

if($serverVersion.name -like "*2003*"){
    $dir = "\\$server" + '\C$\temp'
    remove-item $dir -force -recurse
    new-item -path "\\$server" + '\C$\Windows\temp' -Type Directory
}
elseif($serverVersion.name -like "*2008*"){
    $dir = "\\$server" + '\C$\Windows\Temp'
    remove-item $dir -force -recurse
    New-Item -Path "\\$server" + '\C$\Windows\Temp' -Type Directory 
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:00:54+00:00Added an answer on June 8, 2026 at 7:00 am

    Use the New-Itemcmdlet to create the folder:

    New-Item -Path "\\$server\admin$\Temp" -Type Directory 
    

    You can also use the ‘md’ alias (points to the mkdir function):

    md "\\$server\admin$\Temp" -Force
    

    UPDATE:

    Here’s what I would do: remove the directory. Check the $? variable (returns $true if the last command succeeded), if the command didn’t completed successfully – create the Temp folder, otherwise there must have been an error and the Temp dir still exist.

    Remove-Item  -Path "\\$server\admin$\Temp" -Force -Recurse
    
    if($?)
    {
        New-Item -Path "\\$server\admin$\Temp" -Type Directory 
    }
    

    p.s. You could also add the Force switch to New-Item (or ‘md’) and create the folder regardless of the result of the previous command.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that takes a number of filenames, and then serializes the
I have a script that runs to upload an image, then resize it into
I have a script that is working out the width of the screen -225px
I have a script that detects an android device then redirects to android.html. On
I have a script that is symlinked from one folder to another /var/www/default/index.php which
I have script that removes an h4 element then animates a hidden form in
I have bash script that takes in a couple of arguments tests then and
Lets say that in my flash project I have script that create for me
I have this script that does only the parent folder, it does not rename
I have script that reads remote file content and writes it to local server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.