-1

I am trying to create a script to create a specific folder and subfolders based on user input in SharePoint using Powershell.

I would like to be prompted for a name in a message box. (Message Box) This would create a folder of that name in a specified path. (New-Item -Path c:(UserInput Variable) -ItemType Directory -Force)

Then create a specific set of subfolders in the newly created folder. For Loop (get-child item) I would also like to prevent errors if the folder is already created. -Force Flag

I've been trying to look up how to do this, but i'm not sure how to put it all together.

  • 1
    Michael, welcome to SO. To set your expectations ... please visit the SO [Help Center](https://stackoverflow.com/help) and carefully review at least the following topics: [What topics can I ask about here?](https://stackoverflow.com/help/on-topic), [What types of questions should I avoid asking?](https://stackoverflow.com/help/dont-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Olaf Aug 21 '18 at 23:34
  • 1
    You put it all together by starting with the first step. Write the code that gets the user input. Once you've confirmed they provided input, add the code that creates the outermost folder using that input. Once you've got that working, add the loop that creates the rest of the folders. You've done a great job of defining the tasks. Now just start with the first one, get it working, and then gradually add the rest. – Ken White Aug 22 '18 at 00:21

1 Answers1

0

Here's something that I would do, it's rough and simple. I've taken some functions that already existed here on StackOverflow. Hope this helps you.

Just a polite suggestion but next time, try to include as much of the script as you can even if it's nowhere near complete.

### VARS ###
$subdirs = "Subdir1","Subdir2","Subdir3","Subdir4"

### FUNCTIONS ###

# Stole this function from https://code.adonline.id.au/folder-file-browser-dialogues-powershell/
function Find-Folders {
    [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $browse = New-Object System.Windows.Forms.FolderBrowserDialog
    $browse.SelectedPath = "C:\"
    $browse.ShowNewFolderButton = $false
    $browse.Description = "Choose a parent directory"

    $loop = $true
    while($loop)
    {
        if ($browse.ShowDialog() -eq "OK")
        {
        $loop = $false

        #Insert your script here

        } else
        {
            $res = [System.Windows.Forms.MessageBox]::Show("You clicked Cancel. Would you like to try again or exit?", "Select a location", [System.Windows.Forms.MessageBoxButtons]::RetryCancel)
            if($res -eq "Cancel")
            {
                #Ends script
                return
            }
        }
    }
    $browse.SelectedPath
    $browse.Dispose()
}


# Stole this function from https://stackoverflow.com/questions/30534273/simple-inputbox-function
Function Get-NewDirName (){
    [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    $title = 'New Directory'
    $msg   = 'New directory name:'
    [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
}


### MAIN ###
$parentDir = Find-Folders

if ($parentDir) {

    #Get the new directory name
    $NewDirName = Get-NewDirName
    if ($NewDirName) {

        #Creates the parent directory
        New-Item -Path $parentDir\$NewDirName -ItemType Directory

        #A parent directory was found
        ForEach ($dir in $subdirs) {
            New-Item -Path $parentDir\$NewDirName\$dir -ItemType Directory
        }
    } else {
        Write-Error "No directory name was specified"
    }
} else {
    Write-Error "No parent directory was specified"
}
Jake Nelson
  • 1,748
  • 13
  • 22