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