# I want import Data with .CSV but i can't open my file

**URL:** <https://forums.powershell.org/t/i-want-import-data-with-csv-but-i-cant-open-my-file/19463>\
**Category:** PowerShell Help\
**Created:** [May 30, 2022, 8:28am UTC](https://forums.powershell.org/t/i-want-import-data-with-csv-but-i-cant-open-my-file/19463 "2022-05-30T08:28:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adveonn](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@adveonn](https://forums.powershell.org/u/adveonn)\
**Post date:** [May 30, 2022, 8:28am UTC](https://forums.powershell.org/t/i-want-import-data-with-csv-but-i-cant-open-my-file/19463/1 "2022-05-30T08:28:23Z")

</div>

I create a script for the import of AD-USER with a csv file.  
But actually i can’t open my csv file to add the users to my Active-Directory.

I put the script below

```auto
function menu1 ()
{
#Création des utilisateurs de l'AD

$CSVData = Import-csv $Filepath -Delimiter ';'-Encoding UTF8

# Boucle Foreach pour parcourir le fichier CSV

    Foreach($Utilisateur in $CSVData)
    {
    $UtilisateurPrenom = $Utilisateur.Prenom
    $UtilisateurNom = $Utilisateur.Nom
    $UtilisateurLogin = ($UtilisateurPrenom).Substring(0,2).Tolower() + "." + $UtilisateurNom.ToLower()
    $UtilisateurMotDePasse = "P@ssw0rd"
    $UtilisateurEmail = "UtilisateurLogin@chevrex.bergerie"
    $UtilisateurFonction = $Utilisateur
    $name = $UtilisateurNom + " " + $UtilisateurPrenom
    $path = "OU=Utilisateurs,OU=Service Culturel,OU=Ou_Groupe_Globaux_Maire_Jardres,DC=chevrex,DC=bergerie"   
    # Vérifier la présence de l'utilisateur dans l'AD

        if(Get-ADUser -Filter {SamAccountName -eq $UtilisateurLogin})
        {
        Write-Warning "L'identifiant $UtilisateurLogin existe deja dans l'AD"
        }
        else
        {
        New-ADUser -Name $name `
               -DisplayName "$UtilisateurNom $UtilisateurPrenom" `
               -GivenName UtilisateurPrenom `
               -Surname $UtilisateurNom `
               -SamAccountName $UtilisateurLogin `
               -UserPrincipalName "$UtilisateurLogin@chevrex.bergerie" `
               -EmailAddress $UtilisateurEmail `
               -Title $UtilisateurFonction `
               -Path $path `
               -AccountPassword(ConvertTo-SecureString $UtilisateurMotdePasse -AsPlainText -Force) `
               -ChangePasswordAtLogon $true `
               -Enabled $true

        [System.Windows.Forms.MessageBox]::Show("Création de l'utilisateur : $UtilisateurLogin ($UtilisateurNom $UtilisateurPrenom)", 
        "Création des utilisateurs réussis")
    
        Write-Output "Création de l'utilisateur : $UtilisateurLogin ($UtilisateurNom $UtilisateurPrenom)" -Foregroundcolor green
        }
    }

}

```

If someone can help me.

---

<div class="post-metadata">

**Author:** ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)\
**Post date:** [May 30, 2022, 8:40am UTC](https://forums.powershell.org/t/i-want-import-data-with-csv-but-i-cant-open-my-file/19463/2 "2022-05-30T08:40:43Z")

</div>

Hello adveonn and welcome to the forum.

You don’t include any errors you get from your script, so it’s hard to give any meaningful advice without knowing exactly where things are going wrong.

One thing I’m seeing though is that you seem to have no whitespace between `';'` and the following parameter `-Encoding UTF8` in your `Import-CSV` line. Does it change anything if you add a space there?

Another thing that could potentially be a problem is if you have the CSV-file open in Excel at the same time as you are trying to import it.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 30, 2022, 9:01am UTC](https://forums.powershell.org/t/i-want-import-data-with-csv-but-i-cant-open-my-file/19463/3 "2022-05-30T09:01:45Z")

</div>

You should not use variables inside a function you defined outside of it! I’d provide it as parameter instead. 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/i-want-import-data-with-csv-but-i-cant-open-my-file/19463/4 "2024-05-16T20:17:52Z")

</div>


