Cannot import CSV file into PS script PS version 5.1

Janet E. Lewis 0 Reputation points
2023-08-21T20:25:13.5966667+00:00

I'm trying to edit some attributes by importing them from a CSV file (with ; delimiters). The CSV file has headers username (which is the samaccount name);uid;uidNumber;unixHomedirectory;homeDrive
and the file with the information

The script is being run from drive E. The csv file is in drive E:\ I've made quite sure it is spelled correctly
I am running this via RSAT. I've tried first running $Filepath = "E:\jeltestupdate.csv"
This is the error I'm getting


PS E:\> Import-Csv -Delimiter ";" -Path .\jeltestpdate.csv | Foreach {
    
    $ADUser = Get-ADUser -Filter "username -eq $sAMAccountName"

    if ($ADUser){
        Set-ADUser -Identity $ADUser -username $_.uid -uidNumber $_.uidNumber -unixHomeDirectory $_.unixHomeDirectory -homeDrive $_.homeDrive -homeDirectory $_.homeDirectory
    }else{
        Write-Warning ("Failed to update " + $($_.sAMAccountName))
    }
}
Import-Csv : Could not find file 'E:\jeltestpdate.csv'.
At line:1 char:1
+ Import-Csv -Delimiter ";" -Path .\jeltestpdate.csv | Foreach {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Import-Csv], FileNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-08-21T21:30:48.6166667+00:00

    Either the file (".\jeltestpdate.csv") is not in whatever is the current directory, the file name is incorrect, or the file is in some other directory.

    Try replacing the 1st line of your script with these two lines:

    $p = (Get-Location).Path
    Import-Csv -Delimiter ";" -Path ( ("{0}\{1}" -f $p, "jeltestpdate.csv") ) | ForEach-Object {
    

    If the file still isn't found you'll at least know the full path that was used and you can either adjust the script accordingly or correct the file name.

    0 comments No comments

  2. Anonymous
    2023-08-22T04:30:05.77+00:00

    Hi,

    Do you hide the file name extensions in Windows Explorer? Run Get-ChildItem E:\ and copy the file name from the output or rename the file manually in case there are non printable characters in the file name.

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.