New to PW - Access Denied

Hi all, literally my first day dipping in to PowerSHell. I have done ok so far, but i have hit a hurdle. In short I am currently trying to set all AD users account passwords to be set to expire in 90 days. I have been using this script however I keep getting access denied.

Other scripts have worked fine that are in the c:\ location. I am using Domain admin.

PS C:\WINDOWS> Import-Module activedirectory

$list = Import-CSV c:\passwordneverexpires.csv

forEach ($item in $list) {

$samAccountName = $item.samAccountName

Disable-ADAccount -Identity $samAccountName
}

Import-CSV : Access to the path ‘C:\passwordneverexpires.csv’ is denied.
At line:3 char:9

  • $list = Import-CSV c:\passwordneverexpires.csv
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : OpenError: (:slight_smile: [Import-Csv], UnauthorizedAccessException
    • FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand

Welcome! I hope you’re considering “Learn Windows PowerShell in a Month of Lunches” as a resource.

The error is telling you that whatever user account you’re using to run the PowerShell console doesn’t have access to that file. I suspect you’re not running the shell as Administrator, since users by default don’t have access to C: on newer systems.

Thanks Don,

I will try again, just bit confused why the other scripts I have run on C;\ have worked fine. Will send an update in a bit.

I will look at that resource as well.

Still not working, But my other scripts work when pointing to C:\ for example this works.

Import-Module activedirectory

$list = Import-CSV c:\disableusers.csv

forEach ($item in $list) {

$samAccountName = $item.samAccountName

Disable-ADAccount -Identity $samAccountName
}

Have you looked at the permissions on the file? One thing I’ve done before when testing something over and over is leave the file open. I think the error message is different, like “path not found” but still make sure it’s closed, make sure you have permissions by looking at the permissions tab and try moving it to your desktop or something. By the way, the Month of Lunches book is totally worth it.

Thanks I shall have a look. If i move to the desktop what command would i use to point to it in PS.

sl Desktop

Very odd, I decided to re create the excel file and it’s worked.

thanks all