We help IT Professionals succeed at work.
Get Started

Adding a parameter to CSV output on PS

Last Modified: 2016-02-06
Hi everyone, I have the following script:
$PWDtest = 'P@$$w0rd!'
function Test-ADCredential {
    if (!($UserName) -or !($Password)) {
        Write-Warning 'Test-ADCredential: Please specify both user name and password'
    } else {
        Add-Type -AssemblyName System.DirectoryServices.AccountManagement
        $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('domain')
        $DS.ValidateCredentials($UserName, $Password)

Get-User -ResultSize Unlimited | % {
    If(Test-ADCredential -UserName $_.SamAccountName -Password $PWDtest){
        $_ | Select SamAccountName,WindowsEmailAddress
    } | Export-Csv C:\Results.csv

Open in new window

What this script does is fairly simple, it runs on all the users in my domain, and tried to validate each user with the password specified in $PWDtest and throws all the successful hits into a CSV file.

It was built as part of a security Audit I once ran. I now need to run on another domain, but the leaving sysadmin told me that there are several problematic passwords that they used to use... I want to use on the new domain, but I'd like for the $PWDtest value to appear in the CSV as well and not only the username and the email... How can I achieve that?
Watch Question
Most Valuable Expert 2019
Most Valuable Expert 2018
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE