PowerShell script for creating files and directories


Good evening, messed around on the computer the other day, ended up with creating a script prompting you to create an item.

The full version of the script ended with this:

function New-Items {
    [CmdletBinding()]
    param ()

    process {
        $Path = Read-Host "Where do you want to store your items?"
        function Set-TheLocation($Path) {
            if (Test-Path -Path $Path) {
                Set-Location -Path $Path
                Write-Host -ForegroundColor Green "`nCurrent in the specified directory:"
                Get-ChildItem -Path $Path
            }
            else {
                Write-Host -ForegroundColor Yellow "The $($Path) doesn't exists, 'File Creator' exited."
                exit
            }
        }

        function Create-Items ($InputType, $InputName) {
            do {
                $InputType = Read-Host "`nWhat kind of item do you want to create? File or directory"
                if ($InputType -eq "Stop") {
                    Write-Host -ForegroundColor Yellow "The 'Item Creator' is stopped"
                    break
                }

                $InputName = Read-Host "`nWhat do you want to name it?"
                if ($InputName -eq "Stop") {
                    Write-Host -ForegroundColor Yellow "The 'Item Creator' is stopped"
                    break
                }

                if (!(Test-Path -Path $InputName)) {
                    New-Item -Name $InputName -ItemType $InputType
                    Write-Host -ForegroundColor Green "`nThe $InputType $InputName is successfully created"
                }
                else {
                    Write-Host -ForegroundColor Red "`nThe $InputType $InputName already exists..try with a different name"
                }
            } while ($true)
        }
        Set-TheLocation $Path
        Create-Items $InputType $InputName
    }
}

Lets break it down:

First step is creating an Advanced PowerShell function. This script doesn’t take any parameters, since it only uses input from the user, using the Read-Host cmdlet.

function New-Items {
    [CmdletBinding()]
    param ()

Second step, the first function in the process block. This prompts the user where their want to create the new item(s). If the Path does’t exists the script breaks. Here I should’ve added a another check to see if the user want to create the Path specified. If the Path exists it sets the location to the specified folder Path and lists out all items in that folder.

process {
        $Path = Read-Host "Where do you want to store your items?"
        function Set-TheLocation($Path) {
            if (Test-Path -Path $Path) {
                Set-Location -Path $Path
                Write-Host -ForegroundColor Green "`nCurrent in the specified directory:"
                Get-ChildItem -Path $Path
            }
            else {
                Write-Host -ForegroundColor Yellow "The $($Path) doesn't exists, 'File Creator' exited."
                exit
            }
        }

Third step, the last part in the in the process block. This is a loop that keeps prompting you for what kind of item you want to create and what you want to name it. This goes on and on.. Until you exit the loop by writing Stop when you get prompted.

function Create-Items ($InputType, $InputName) {
            do {
                $InputType = Read-Host "`nWhat kind of item do you want to create? File or directory"
                if ($InputType -eq "Stop") {
                    Write-Host -ForegroundColor Yellow "The 'Item Creator' is stopped"
                    break
                }

                $InputName = Read-Host "`nWhat do you want to name it?"
                if ($InputName -eq "Stop") {
                    Write-Host -ForegroundColor Yellow "The 'Item Creator' is stopped"
                    break
                }

                if (!(Test-Path -Path $InputName)) {
                    New-Item -Name $InputName -ItemType $InputType
                    Write-Host -ForegroundColor Green "`nThe $InputType $InputName is successfully created"
                }
                else {
                    Write-Host -ForegroundColor Red "`nThe $InputType $InputName already exists..try with a different name"
                }
            } while ($true)
        }
        Set-TheLocation $Path
        Create-Items $InputType $InputName

    }
}

To run the function use . .\New-Items.ps1 to load it into memory, then trigger the command with New-Items. When executing the script it can look something like this:

Image of the execution of the script New-Items. A demo of how it is used.

Well, I guess that’s it. There is plenty of things I should add to improve the script, but for now, this was just a goofing around project for tonight. Have a great evening!


For More Content See the Latest Posts