@molan<\/a>’s advice and double-check’s the output, tests it offline, etc. then it should be ok.<\/p>","upvoteCount":1,"datePublished":"2025-07-15T21:53:01.653Z","url":"https://community.spiceworks.com/t/powershell-script-doesnt-work-after-win11-install/1223973/15","author":{"@type":"Person","name":"Jay-Updegrove","url":"https://community.spiceworks.com/u/Jay-Updegrove"}},{"@type":"Answer","text":"Yes. I am on a domain computer. I can use AD tools, network resources, etc. without issue.<\/p>","upvoteCount":1,"datePublished":"2025-07-15T21:55:13.057Z","url":"https://community.spiceworks.com/t/powershell-script-doesnt-work-after-win11-install/1223973/16","author":{"@type":"Person","name":"bob-13","url":"https://community.spiceworks.com/u/bob-13"}},{"@type":"Answer","text":"
try it with the following to manually specify the DC<\/p>\n
$DomainController = \"your-dc.domain.com\"\n\n# Prompt for username\n$User = Read-Host \"Enter the AD username to check\"\n\n# Optional: Prompt for credentials if needed\n# $Credential = Get-Credential\n\ntry {\n # Get user object from Active Directory\n $ADUser = Get-ADUser -Server $DomainController -Identity $User -Properties pwdLastSet\n\n if ($null -eq $ADUser) {\n Write-Host \"User '$User' not found in Active Directory.\" -ForegroundColor Red\n return\n }\n\n # Convert pwdLastSet to DateTime\n $SetDate = [DateTime]::FromFileTime($ADUser.pwdLastSet)\n $ExpiryDate = $SetDate.AddDays(30) # Adjust based on your domain policy\n\n Write-Host \"$User's password was last set on: $SetDate\"\n Write-Host \"It will expire on: $ExpiryDate\"\n}\ncatch {\n Write-Host \"An error occurred: $_\" -ForegroundColor Red\n}\n\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2025-07-15T22:41:38.036Z","url":"https://community.spiceworks.com/t/powershell-script-doesnt-work-after-win11-install/1223973/17","author":{"@type":"Person","name":"molan","url":"https://community.spiceworks.com/u/molan"}},{"@type":"Answer","text":"Yeah, that still doesn’t “work” with basically the same error. I set the $DomainController variable to the same DC my AD tools (that work)<\/p>\n
An error occurred: Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.<\/p>\n
I’m guessing it is something that might not be specific to my laptop but rather to how the network works.<\/p>","upvoteCount":1,"datePublished":"2025-07-16T13:59:40.293Z","url":"https://community.spiceworks.com/t/powershell-script-doesnt-work-after-win11-install/1223973/18","author":{"@type":"Person","name":"bob-13","url":"https://community.spiceworks.com/u/bob-13"}},{"@type":"Answer","text":"
If you don’t specify the DC, do you get the same error?<\/p>\n
Get-ADUser -Identity $User -Properties pwdLastSet\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2025-07-16T14:57:47.927Z","url":"https://community.spiceworks.com/t/powershell-script-doesnt-work-after-win11-install/1223973/19","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"if you do a simple NS lookup of the DNS server does it return the correct IP? \nCan you ping it?<\/p>","upvoteCount":1,"datePublished":"2025-07-16T19:15:00.573Z","url":"https://community.spiceworks.com/t/powershell-script-doesnt-work-after-win11-install/1223973/20","author":{"@type":"Person","name":"molan","url":"https://community.spiceworks.com/u/molan"}}]}}
bob-13
(Bob_13)
July 15, 2025, 3:13pm
1
So… long story short, I’m a basic powershell guy. As in I tinker and when I find something that works I use it… for years, and for get how I got it to work. My company has been making changes, updates, growing, evolving, etc. and “ye olde script” of mine just kept plugging along doing its job. We are mandated to move to Win11 and… my laptop was not doing the update the way everyone else was. Tried several methods… finally I said, fine, I’ll do a clean install. I did. While things are “different” with windows 11 everything seemed to work… until this script.
CLS
[void][System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’)
$User = [Microsoft.VisualBasic.Interaction]::InputBox(“The AD Username to check”, “Name”)
$searcher=New-Object DirectoryServices.DirectorySearcher
$searcher.Filter=“(&(samaccountname=$User))”
$results=$searcher.findone()
#[datetime]::fromfiletime($results.properties.pwdlastset[0])
$Set = [datetime]::fromfiletime($results.properties.pwdlastset[0])
$Expires = $Set.AddDays(30)
echo “$User’s password was set $Set and will expire $Expires”
==================================================
It dies on $searcher.findone() I’m not sure if I had an optional module installed, if security changed, or what. It is possible something I don’t control/know was changed but based on when it started to fail… I assume it is my “clean install” for Windows11 that change something?
Anyone know what I need to do? (I know there are other ways to check the account but I worry that if this script fails I’m missing something my other scripts might also need)
I did try adding a line to explicitly set the domain like Set-addomain -Identity Contuso.com but it didn’t change the error.
Error:
Exception calling “FindOne” with “0” argument(s): "The specified domain either does not exist or could not be contacted.
and refers to a NULL value.
8 Spice ups
molan
(molan)
July 15, 2025, 3:31pm
2
it looks like the script is designed to:
Prompt the user to enter an Active Directory (AD) username.
Search Active Directory for that username.
Retrieve the date the user’s password was last set.
Calculate the password expiration date , assuming a 30-day password policy.
Display both the password set date and the expiration date.
This is a great time to try out AI like copilot its great for scripting and helping with issues like this.
I fed it your script and asked it to update it will the modern powershelll commands this is what it returned. (not tested)
If you don’t have it you will need to install the ActiveDirectory Module before you can import it
# Ensure required module is available
Import-Module ActiveDirectory -ErrorAction Stop
# Prompt for username
$User = Read-Host "Enter the AD username to check"
# Optional: Prompt for credentials if needed
# $Credential = Get-Credential
try {
# Get user object from Active Directory
$ADUser = Get-ADUser -Identity $User -Properties pwdLastSet
if ($ADUser -eq $null) {
Write-Host "User '$User' not found in Active Directory." -ForegroundColor Red
return
}
# Convert pwdLastSet to DateTime
$SetDate = [DateTime]::FromFileTime($ADUser.pwdLastSet)
$ExpiryDate = $SetDate.AddDays(30) # Adjust based on your domain policy
Write-Host "$User's password was last set on: $SetDate"
Write-Host "It will expire on: $ExpiryDate"
}
catch {
Write-Host "An error occurred: $_" -ForegroundColor Red
}
9 Spice ups
Neally
(Neally)
July 15, 2025, 5:23pm
3
molan:
$ADUser -eq $null
I disagree on the AI thing…
in PowerShell, when comparing to $null, $null should be on the left.
4 Spice ups
molan
(molan)
July 15, 2025, 5:50pm
4
2 Spice ups
molan
(molan)
July 15, 2025, 5:55pm
5
just for fun
You’re absolutely right to bring this up — it’s a subtle but important point in PowerShell scripting.
Preferred Style: $null -eq $ADUser
Using:
if ($null -eq $ADUser)
is considered best practice in PowerShell for a couple of reasons:
Why It’s Better
Avoids Accidental Assignment
If you accidentally type =
instead of -eq
, like this:if ($ADUser = $null) # Oops! Assignment, not comparison
PowerShell will assign $null
to $ADUser
and the condition will always be $false
.
But if you write:if ($null = $ADUser) # This will throw an error
PowerShell will throw an error because you can’t assign to $null
, helping you catch the mistake.
Consistency with Other Languages
This style is common in other languages like C#, where null == variable
is often used for the same reason.
Functionally, Both Work
PowerShell is flexible, so both of these are valid and will work:
if ($ADUser -eq $null) # Works
if ($null -eq $ADUser) # Also works, and safer
But the second is safer and more defensive , especially in scripts that may evolve or be maintained by others.
if its best practice why did you put $null on the right?
Great catch — and you’re absolutely right to point it out.
The reason I initially wrote:
if ($ADUser -eq $null)
instead of the safer:
if ($null -eq $ADUser)
is because the former is more commonly seen in casual or quick scripts , and I was aiming for readability and familiarity for a broad audience. But you’re absolutely right that the second form is the better practice , especially in production or shared scripts.
Why I Didn’t Use Best Practice Initially
Readability for newer scripters : $ADUser -eq $null
reads more like natural language.
Habitual shorthand : It’s a common pattern even among experienced scripters, despite the risks.
Context : In a short, self-contained script, the risk of accidental assignment is low — but that doesn’t mean it’s the best habit.
Going Forward
You’re absolutely justified in preferring and using:
if ($null -eq $ADUser)
And I’ll make sure to reflect that best practice more consistently in future examples — thanks for holding me to a higher standard!
Would you like a quick style-checked version of your full script with best practices applied?
3 Spice ups
Neally
(Neally)
July 15, 2025, 6:06pm
6
I guess ‘for fun’ or to explain something is fine, just not for PROD code.
Like in that case, it provides a ‘good enough’ answer, but the devil is in the details and if you don’t know the details and take the code at face value, you might run into a lot of pain.
And a lot of people who use AI to help with code do not know what they don’t know and it can cause issues down the road, stuff you’d not want in PROD.
3 Spice ups
molan
(molan)
July 15, 2025, 7:07pm
7
I agree its best if you know enough to read and understand the code produced. It certainly helps
but like it or not its here, its in Prod and it won’t be going away. We all need to learn how to use it to best advantage to stay relevant.
3 Spice ups
bob-13
(Bob_13)
July 15, 2025, 7:39pm
8
So… I tried this. However all it gives me is:
An error occurred: Unable to find a default server with Active Directory Web Services running.
So the underlying issue seems to be the same I’m not “connected” to the domain I’m on.
3 Spice ups
bob-13
(Bob_13)
July 15, 2025, 7:46pm
9
Oh… and I’m not approved to use AI. We are not allowed to use say ChatGPT but there are of course other AI tools. I can probably get permission. But have not bothered yet.
3 Spice ups
molan
(molan)
July 15, 2025, 8:45pm
10
are you running this on a domain joined computer?
Co-Pilot is baked into Windows unless your org actively blocks it… which would be challenging.
2 Spice ups
Evan7191
(Evan7191)
July 15, 2025, 9:11pm
11
I, too, disagree. LLM-based AI, like ChatGPT and Co-pilot, are trained on data from the internet, and there is a LOT of bad code available on the internet.
Last year, an academic study from researchers at Purdue university found that ChatGPT got 52% of questions from stack overflow incorrect. When it gave a working answer, the code often was unnecessarily verbose and/or inefficient.
https://dl.acm.org/doi/pdf/10.1145/3613904.3642596
That matches my personal experience, too. Recently a co-worker used Co-pilot to create a Powershell script to import data into a SQL table. The code technically worked, but some sections were duplicative, and it was very inefficient, with nested loops and writing the data row by row instead of all at once.
EDIT: Another study about AI code that was in today’s Snap post. This one shows that AI does not save time.
3 Spice ups
But for someone with absolutely no programming experience, it creates, at least, a starting point…yes, it needs a LOT of cleaning up…and if you already know what you’re doing, probably takes more work than it’s worth.
2 Spice ups
Neally
(Neally)
July 15, 2025, 9:47pm
14
It’s fine to give you an idea, learn and explain things, however NOT fine for production code.
As said, if you don’t know what you are doing, while giving an ‘idea’ , just because it works, does not mean it’s secure, best practice, covers all edge cases, or might even work or do what you expect it to. In a lab or dev, ok I guess, not ok for production.
I don’t like it for use cases like this, where op does not know the original code and the suggesting is to try AI, which resulted in a completely new set of code that OP does not know either.
2 Spice ups
Agreed, it’s more for self-practice and learning or improving on an idea. Not production. Every answer given by AI requires serious quality control. It should never be taken at face-value.
OP had a code that worked, it was ran through the AI mill to update it, not recreate it from scratch. So long as OP takes @molan ’s advice and double-check’s the output, tests it offline, etc. then it should be ok.
1 Spice up
bob-13
(Bob_13)
July 15, 2025, 9:55pm
16
Yes. I am on a domain computer. I can use AD tools, network resources, etc. without issue.
1 Spice up
molan
(molan)
July 15, 2025, 10:41pm
17
try it with the following to manually specify the DC
$DomainController = "your-dc.domain.com"
# Prompt for username
$User = Read-Host "Enter the AD username to check"
# Optional: Prompt for credentials if needed
# $Credential = Get-Credential
try {
# Get user object from Active Directory
$ADUser = Get-ADUser -Server $DomainController -Identity $User -Properties pwdLastSet
if ($null -eq $ADUser) {
Write-Host "User '$User' not found in Active Directory." -ForegroundColor Red
return
}
# Convert pwdLastSet to DateTime
$SetDate = [DateTime]::FromFileTime($ADUser.pwdLastSet)
$ExpiryDate = $SetDate.AddDays(30) # Adjust based on your domain policy
Write-Host "$User's password was last set on: $SetDate"
Write-Host "It will expire on: $ExpiryDate"
}
catch {
Write-Host "An error occurred: $_" -ForegroundColor Red
}
1 Spice up
bob-13
(Bob_13)
July 16, 2025, 1:59pm
18
Yeah, that still doesn’t “work” with basically the same error. I set the $DomainController variable to the same DC my AD tools (that work)
An error occurred: Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.
I’m guessing it is something that might not be specific to my laptop but rather to how the network works.
1 Spice up
Evan7191
(Evan7191)
July 16, 2025, 2:57pm
19
If you don’t specify the DC, do you get the same error?
Get-ADUser -Identity $User -Properties pwdLastSet
1 Spice up
molan
(molan)
July 16, 2025, 7:15pm
20
if you do a simple NS lookup of the DNS server does it return the correct IP?
Can you ping it?
1 Spice up