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 
    }
Valrok
  • 1,514
  • 7
  • 30
  • 49

2 Answers2

1

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.

Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Hi Shay, quick question for ya: what does admin$ do? Is that a different method that allows to get any disk rather than just the C Disk? Never seen that before and curious – Valrok Jul 23 '12 at 20:07
  • 2
    admin$ is an adminisrative share (created by windows) that points to the windows directory on each server. http://en.wikipedia.org/wiki/Administrative_share – Shay Levy Jul 23 '12 at 20:23
  • Interesting.. In my case however earlier in the script the user decides which specific server to work on. Was wondering though, would just doing that and the remove-item with a recurse for both W2K3 and W2k8 do the same thing as all the code that I have written at the moment? Would love to cut all that down to just two lines for simplicity. – Valrok Jul 23 '12 at 20:50
  • It should work the same for both server versions. Try this agaianst 2003/2208, from your run box type: \\server2003\admin$ and \\server2008\admin$, both commands should open the remote Windows folder. – Shay Levy Jul 24 '12 at 07:40
  • I tested your method and it works similar to what I had before but cuts down the amount of code! I'm running into an error however in terms of when I'm deleting everything, there's one directory inside of Temp which isn't empty, and I'm unable to recurse and delete that one item. Was wondering if there's any methods to get around this so that I can then recreate a new Temp directory – Valrok Jul 24 '12 at 13:40
  • Updated my answer, give it a try. – Shay Levy Jul 24 '12 at 13:53
  • Thanks Shay! Never knew about the $? variable either, nice little tip for using in the future! – Valrok Jul 24 '12 at 14:56
  • Hi Shay, been using this script for a couple days now and everything works fine, except for a small issue that has me confused on why it doesn't work: I thought that the use of -recurse and -force was the equivalent of a deltree or rm -r command, however the script won't delete any directories with things inside of it. Would you happen to know why Powershell does this? – Valrok Jul 27 '12 at 20:07
  • The command should remove everything unless something is preventing it from doing that. Do you get any error messages? – Shay Levy Jul 28 '12 at 11:28
  • I get the following: "Remove-Item : Cannot remove item \\SERVER\C$\WINDOWS\Temp: The directory is not empty." This only happens when trying to delete directories inside of the temp folder that have things inside of it. – Valrok Jul 30 '12 at 13:01
  • And the -Recurse switch doesn't help? – Shay Levy Jul 30 '12 at 13:19
  • Nope, for some reason the -Recurse will go into the temp folder and delete everything inside as expected, except for directories inside the folder that are full. – Valrok Jul 30 '12 at 13:33
1

Why don't you simply remove temp folder with a recursive remove-item

# Remove recursively
Remove-Item  -Path "\\$server\admin$\Temp" -Force -Recurse
# Create another one
New-Item -Path "\\$server\admin$\Temp" -Type Directory 
JPBlanc
  • 70,406
  • 17
  • 130
  • 175