New-PSDriveRootFolder.ps1


Description

Purpose

Creates PowerShell drives for all folders in a given path.

Detailed Description

The New-PSDriveRootFolder function creates PowerShell drives for all folders in a specified root folder. It iterates through each folder in the root path, creating a PSDrive for each one. The function validates the specified path and handles errors gracefully.

Back to Top

Usage

Example 1

New-PSDriveRootFolder -FolderPath "C:\Users\username\Documents\WindowsPowerShell\Modules"

Creates PS Drives for all subfolders in the specified path.

Back to Top

Notes

Ensure you have the necessary permissions to create PS Drives for the specified folders.

Back to Top


Script

function New-PSDriveRootFolder {
	<#
    .SYNOPSIS
    Creates PowerShell drives for all folders in a given path.

    .DESCRIPTION
    The `New-PSDriveRootFolder` function creates PowerShell drives for all folders in a specified root folder. 
    It iterates through each folder in the root path, creating a PSDrive for each one. The function validates 
    the specified path and handles errors gracefully.

    .PARAMETER FolderPath
    The root folder for which to create PS Drives.

    .EXAMPLE
    New-PSDriveRootFolder -FolderPath "C:\Users\username\Documents\WindowsPowerShell\Modules"
    Creates PS Drives for all subfolders in the specified path.

    .NOTES
    Ensure you have the necessary permissions to create PS Drives for the specified folders.

    .LINK
    # Add relevant links if necessary
    #>

	[CmdletBinding()]
	param (
		[Parameter(Mandatory = $true)]
		[ValidateScript({
				if (Test-Path $_ -PathType Container) {
					$true
				}
				else {
					throw "Path `$_` is not a valid directory."
				}
			})]
		[string]$FolderPath
	)

	# Get all directories in the specified path
	$PSDrivePaths = Get-ChildItem -Path "$FolderPath" -Directory

	# Initialize progress tracking variables
	$totalItems = $PSDrivePaths.Count
	$currentItem = 0

	foreach ($item in $PSDrivePaths) {
		$currentItem++
		Write-Progress -Activity "Creating PS Drives" -Status "Processing Item $currentItem of $totalItems" -PercentComplete (($currentItem / $totalItems) * 100)

		try {
			# Ensure the path exists
			if (Test-Path -Path $item.FullName) {
				# Generate a valid drive name by removing invalid characters
				$driveName = $item.Name -replace '[;~\/\.:]', ''

				# Check if the PSDrive already exists and handle naming conflicts
				$originalDriveName = $driveName
				$index = 1
				while (Get-PSDrive -Name $driveName -ErrorAction SilentlyContinue) {
					$driveName = "$originalDriveName$index"
					$index++
				}

				# Create the new PSDrive
				New-PSDrive -Name $driveName -PSProvider "FileSystem" -Root $item.FullName -Scope Global
				Write-Verbose "Drive $driveName created successfully."
			}
		}
		catch {
			Write-Warning "Error creating drive {$driveName}: $_"
		}
	}
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top