I'm trying to powershell script working with my admin credentials. I am still farily new to the "powershell script world" so I have been looking through existing scripts and taping / modifying one together.

I have the string :

$targetdomain = ""
Write-Verbose "Getting credentials"
$domaincredential = Get-Credential -UserName "$targetdomain\$env:USERNAME-ADM" -Message "Use a credential that has permission to create users in the target domain $targetdomain"

These promps for my -adm account information which I then type in. But when I try to run the script it says

Set-ADUser : Insufficient access rights to perform the operation

If open powershell by "right clicking on the icon->Run as administrator->Enter credentials" and then copy the script it then it works like a charm.

So my question is. What am I doing wrong? I'm guessing it is the credentials line but I simply can't figure out what is wrong. I have also tried with "$cred = Get-Credential domain\username" but that doesn't work either.

You can see the full script below:

$targetdomain = ""
Write-Verbose "Getting credentials"
$domaincredential = Get-Credential -UserName "$targetdomain\$env:USERNAME-ADM" -Message "Use a credential that has permission to create users in the target domain $targetdomain"

foreach($line in Get-Content "request.txt") {

if ($line.length -eq 0) {
$line = "  "

    if($line -match "First name of user: ") {
            $name = $line.Substring(21).trim()

    if($line -match "  Last name of user: ") {
            $lastname = $line.Substring(22).trim()

    if ($line.Substring(0,1) -eq "" -and $line.Length -eq 6) {
            $LID = $line.trim()
Set-ADUser -Identity $LID -Add @{ProxyAddresses = ""}
Set-ADUser -Identity $LID -Add @{ProxyAddresses = ""}
  • If any mods sees this. Please delete this question fully. Not going to go into too much details only that this has to be removed due to laws in my former firm/country. – B.Frederik Jul 12 '18 at 11:44
  • @Sathya can you please make a chat with me? – B.Frederik Jul 12 '18 at 11:47
  • @B.Frederik You need to contact the moderators and ask them to redact the sensitive information instead of simply adding another revision. The original data will still be available if you simply edit your question. – Secespitus Jul 12 '18 at 11:50
  • @B.Frederik please [flag] the question and mention what should be redacted – Sathyajith Bhat Jul 12 '18 at 11:52
  • @Sathya Flagged. The questions needs to either be fully redacted all of it or completely removed. These were the only info I got from now my former work. – B.Frederik Jul 12 '18 at 11:55

Look carefully at the code: you are asking for user's credentials, save them into $domaincredential variable which is not used later.

Add -Credential $domaincredential argument to Set-ADuser cmdlet calls:

Set-ADUser -Identity $LID `
           -Add @{ProxyAddresses = "NOTES:$name $lastname/DK$LID/UNIBANK"} `
           -Credential $domaincredential
  • Hello, That works for me. But my colleagues get the error: "Set-ADUser : The term 'Set-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. " Any idea what might be the issue? – B.Frederik Jun 1 '18 at 12:22
  • 1
    Because they don't have Powershell AD management module installed: docs.microsoft.com/en-us/powershell/module/addsadministration/…. if the answer solves your original issue, feel free to mark it as an answer. if you have further questions you can create them separately ;) – maoizm Jun 1 '18 at 12:38
  • Would sound strange if that was the case. I have a fresh standard powershell installation just like my colleagues, and various "AD" scripts work for them, copying users adding mass users to groups etc. I'll try again on Monday with what you said. And if it doesn't work then i'll solve this and make a new topic. – B.Frederik Jun 1 '18 at 14:08
  • There can be a lot of reasons, eg. on Windows Server it it included by default, on Windows desktop you have to install it manually. Version of Windows matters as well. Every installation of Windows is unique unless you have put a lot of non-default efforts to make them the same – maoizm Jun 1 '18 at 14:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.