github twitter linkedin
30 Oct 2013
One minute read

If you have to do it more than once, script it and blog about it…

function Create-ServerDirectory
        # server name or instance name of SQL Server to run assessment against

            HelpMessage='Provide a single or comma separated list of server names.' )]

    foreach ($s in $server) {
        $dirName = $s.Replace("\","_")
        $fullPath = "$location\$dirName"
        if(Test-Path $fullPath) {
            Write-Warning "The path $fullPath already exist"
        else {
            New-Item -Path $fullPath -ItemType Directory | Out-Null
            if(Test-Path $fullPath){
                Write-Host "$fullPath created successfully"
            else {
                Write-Warning "Some issue occurred creating the directory"

So a few examples:

Create-ServerDirectory –location c:\temp –server "Server1","Server2","Server1\Instance1","Server3"


$list = “Server1”,”Server2”,”Server1\Instance1”,”Server3”
Create-ServerDirectory –location c:\temp –server $list


"Some cmdlet that passes your server name down pipeline" | foreach {Create-ServerDirectory –location c:\temp –server $_}

Carry on…

Back to posts