Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
121 lines (96 sloc) 3.81 KB
Create a default set of folders in Configuration Manager
Will accept a CSV file of the desired folder structure and create the folders in Configuration Manager.
3-character site code
Full or relative path to the CSV file containing the desired folder structure
.PARAMETER CustDirectory
[Optional] allows a default top level directory to be defined. This is useful
for a hierarchy that is shared between distinct departments or organizational units.
Author: Mark Allen
Created: 27/10/2016
.\New-CMFolders.ps1 -SiteCode PR1 -Path .\MyFolderStructure.csv
Will create the folders in the root node of each object type.
EG Application node > MyDir
.\New-CMFolders.ps1 -SiteCode PR1 -Path .\MyFolderStructure.csv -CustDirectory "Test"
Will create a "Test" folder in the root node of each object type; subfolders will be
created with the relevant "Test" folder.
EG Application node > Test > MyDir
[CmdletBinding( SupportsShouldProcess = $False, ConfirmImpact = "None", DefaultParameterSetName = "" ) ]
[ValidateScript({Test-Path $(Split-Path $_) -PathType 'Container'})]
$CurrentDrive = "$($pwd.Drive.Name):"
$FolderStructure = Import-Csv -Path $Path
Import-Module ($env:SMS_ADMIN_UI_PATH.Substring(0,$env:SMS_ADMIN_UI_PATH.Length5) + '\ConfigurationManager.psd1') | Out-Null
#CM12 cmdlets need to be run from the CM12 drive
Set-Location "$($SiteCode):" | Out-Null
if (-not (Get-PSDrive -Name $SiteCode))
Write-Error "There was a problem loading the Configuration Manager powershell module and accessing the site's PSDrive."
exit 1
function New-CMFolder
param (
# catch NULL or empty top level directories
if($Path.substring($Path.length -1, 1) -eq '\') { return $false }
if (!(Test-Path $Path))
Write-Host -ForegroundColor Yellow ("Creating folder $Path")
New-Item -Path (Split-Path $Path) -Name (Split-Path $Path -Leaf) -ItemType Directory
if (!(Test-Path $Path))
Write-Host -ForegroundColor Red ("Failed to create $Path")
return $false
Write-Host -ForegroundColor Green ("$Path exists")
return $true
foreach ($Folder in ($FolderStructure.ParentRoot | Get-Unique))
$ThisRoot = $SiteCode + ':\' + $Folder
$ThisRoot = $ThisRoot + '\' + $CustDirectory
if(!(New-CMFolder $ThisRoot)) {continue}
foreach ($ChildOne in (($FolderStructure | Where {$_.ParentRoot -eq $Folder}).ChildOne) | Get-Unique)
$ChildOnePath = $ThisRoot + '\' + $ChildOne
if(!(New-CMFolder $ChildOnePath)) {continue}
foreach ($ChildTwo in (($FolderStructure | Where {$_.ChildOne -eq $ChildOne -and $_.ParentRoot -eq $Folder}).ChildTwo) | Get-Unique)
$ChildTwoPath = $ChildOnePath + '\' + $ChildTwo
if(!(New-CMFolder $ChildTwoPath)) {continue}
foreach ($ChildThree in (($FolderStructure | Where {$_.ChildTwo -eq $ChildTwo -and $_.ChildOne -eq $ChildOne -and $_.ParentRoot -eq $Folder}).ChildThree) | Get-Unique)
$ChildThreePath = $ChildTwoPath + '\' + $ChildThree
if(!(New-CMFolder $ChildThreePath)) {continue}
# reset the current directory
Set-Location $CurrentDrive
start-sleep 3
Get-Module -Name ConfigurationManager | remove-module -force
You can’t perform that action at this time.