Couple of functions to Create and Remove Folders

Topics: Archive - Toolkit Extensions
Jun 2, 2014 at 3:47 AM
Just a couple of functions that will Create and Remove folders that I have added to the toolkit extensions script.

They also add to the main log file and will show in the 'help' as well. I have sort of cheated a bit as took one of the functions out of the main script and 'modded' them to produce what I wanted.

They have been tested with the AutoIt script I created that is in the Deployment scripts section.

Function Create-Folder {
<#
.SYNOPSIS
Function to create a folder.
.DESCRIPTION
Function to create a folder if it does not exist.
.EXAMPLE
Create-Folder -Path "$envWinDir\System32"
.PARAMETER Path
Path of the folder you want to create
.PARAMETER ContinueOnError
Continue if an error is encountered
.NOTES
.LINK
Http://psappdeploytoolkit.codeplex.com

> Param(

    [Parameter(Mandatory = $true)]
    [string]$Path = $(throw "Path param required"),
    [Parameter(Mandatory = $true)]
    [boolean] $ContinueOnError = $true
)

Write-Log "Testing if folder [$Path] exists..."
$CheckFolder = Test-Path -PathType Container $Path

If ($CheckFolder -eq $False) {
    Write-Log "Creating Folder [$Path]..."
    New-Item $Path -type Directory
}
Else {
    Write-Log "$Path folder already exists..."
}
Trap [Exception] {
    If ($ContinueOnError -eq $true) {
        Write-Log $("Could not create folder [$path]:" + $_.Exception.Message)
        Continue
    }
    Else {
        Throw $("Could not create folder [$path]:" + $_.Exception.Message)
    }
}
}

Function Remove-Folder {
<#
.SYNOPSIS
Function to remove a folder and files if they exist.
.DESCRIPTION
Function to remove a folder and all files recursively in a given path.
.EXAMPLE
SIRemove-Folder -Path "$envWinDir\Downloaded Program Files\*"
.EXAMPLE
Remove-File -Path "$envWinDir\Downloaded Program Files\*" -Recurse
.PARAMETER Path
Path of the folder you want to remove
.PARAMETER Recurse
Optionally, remove all folder and files recursively in a directory
.PARAMETER ContinueOnError
Continue if an error is encountered
.NOTES
.LINK
Http://psappdeploytoolkit.codeplex.com

>

Param(
    [Parameter(Mandatory = $true)]
    [string] $Path = $(throw "Path Param required"),
    [switch] $Recurse,
    [boolean] $ContinueOnError = $true
)

Write-Log "Testing if folder [$Path] exists..."
$CheckFolder = Test-Path -PathType Container $Path

If ($CheckFolder -ne $False) {
   Write-Log "Deleting Folder(s) and Files [$path]..."
   If ($Recurse) {
       Remove-Item -Path "$path" -ErrorAction "STOP" -Force -Recurse | Out-Null
   }
   Else {
      Remove-Item -Path "$path" -ErrorAction "STOP" -Force | Out-Null
   }
Trap [Exception] {
    If ($ContinueOnError -eq $true) {
        Write-Log $("Could not delete folder and files [$path]:" + $_.Exception.Message)
        Continue
    }
    Else {
        Throw $("Could not delete folder and files [$path]:" + $_.Exception.Message)
    }
    }
}
}
Coordinator
Jun 9, 2014 at 1:46 PM
Hey Sue,

Mind mailing me these functions? The formatting above is all over the place hehe. But I'd like to get these embedded as core functions. Great work!

Cheers, Dan
Coordinator
Jun 10, 2014 at 1:47 PM
Hey Sue,

I've taken these and integrated them into the latest source - I've made a few minor changes though:
  • Renamed Create-Folder to New-Folder to follow PowerShell verb naming scheme
  • Fixed mandatory -ContinueOnError parameter on New-Folder
  • Removed -Recurse parameter on Remove-Folder. Done by default. If a folder isn't empty and you didn't specify -Recurse, the whole script would hang waiting for user input
  • Switched from Trap to Try / Catch on both to be in line with new exception handling across all functions
Thanks for your work, credits in the changelog :)

Dan