1

Hi I have the following powershell script to create folders with folder names from a samAccountName value in a spreadsheet. I have encountered a stumbling block with this

I have split up the variables to make it easier to develop

Import-Csv "C:\Users\bhabib\source\repos\TSS---New-Starter-automation-master\adusers.csv" ';' | ForEach-Object {
$upn = $_.SamAccountName + “@domain.com” 
        $test = $_."samAccountName"
        $folder = "C:\Users\bhabib\test"+$test
        $path = $folder 
        New-Item -Path $folder -Type Directory 
}

What this actually does is create the child test folder under bhabib with the name test, it does not take the samAccountName for the spreadsheet, it just creates "test". Also if i run the script again it causes an exception because it is creating "test" again and not the samAccountName. Here is the exception

New-Item : An item with the specified name C:\Users\bhabib\test already exists.
At C:\Users\bhabib\source\repos\TSS---New-Starter-automation-master\folder creation.ps1:6 char:9
+         New-Item -Path $folder -Type Directory
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (C:\Users\bhabib\test:String) [New-Item], IOException
    + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand

My question is how do i fix the issue where it does not take the samAccountName from the spreadsheet because that will fix it. The spreadsheet itself is not broken because I have it working with a create ad users script

1 Answer 1

1

I have generated a test csv file and tried to create a new folder from this, it worked:

@'
col1;samAccountName
value1;mynewfolder
'@ | Out-File text.csv

Import-Csv text.csv ';' | %{New-Item -Path ("C:\Users\bhabib\test\" + $_."samAccountName") -Type Directory -Force}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Benoit, that is odd because i just ran that and it created test again but not the folder with the samAccountName, it seems to be getting the name from "\bhabib\test\"
I am sure you should have aldready checked but is your csv file correctly imported ? Do you get all the folder names you want if you just execute Import-Csv "C:\Users\bhabib\source\repos\TSS---New-Starter-automation-master\adusers.csv" ';' | %{$_."samAccountName"} ?
hi Benoit i just ran the import line only but i do not get any output however my active directory script to create users works fine with the same csv
I think the issue is with -path, if i manually set the name then it still throws the exception above
I have managed to get this working by combining it with my users script, seems to work now, I have moved onto creating the folder on the network share but it says the path is illegal
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.