locked
Feeding text file to Powershell Scritp RRS feed

  • Question

  • I am trying to run a powershell script I found that, running by itself gets all computers in the domain. However I have a .txt file that has specific computer accounts that I want to feed to the script so it only runs against those accounts. below is the script command....

    Get-ADComputer -Filter * -Properties * | select samaccountname,canonicalname,operatingSystem,lastLogonTimestamp

    I want to be able to have it reference a computers.txt so the above command only runs against those names. Any help is much appreciated.

    THanks.


    Chad Guiney

    Wednesday, July 3, 2019 4:24 PM

Answers

  • Get-Content <PATH TO TXT> | foreach{
    Get-ADComputer $_ -Properties * | select samaccountname,canonicalname,operatingSystem,lastLogonTimestamp
    }

    Try above, the computernames should be in a new line in the in the .txt file as:

    Computer1
    Computer2
    Computer3
    .....

    Also please make sure there are no leading or trailing spaces in the .txt file.


    • Proposed as answer by Vector BCO Wednesday, July 3, 2019 4:50 PM
    • Marked as answer by Charlie4872 Wednesday, July 3, 2019 4:54 PM
    Wednesday, July 3, 2019 4:36 PM

All replies

  • Get-Content <PATH TO TXT> | foreach{
    Get-ADComputer $_ -Properties * | select samaccountname,canonicalname,operatingSystem,lastLogonTimestamp
    }

    Try above, the computernames should be in a new line in the in the .txt file as:

    Computer1
    Computer2
    Computer3
    .....

    Also please make sure there are no leading or trailing spaces in the .txt file.


    • Proposed as answer by Vector BCO Wednesday, July 3, 2019 4:50 PM
    • Marked as answer by Charlie4872 Wednesday, July 3, 2019 4:54 PM
    Wednesday, July 3, 2019 4:36 PM
  • Perfect that worked thank you!

    Chad Guiney

    Wednesday, July 3, 2019 4:54 PM
  • This should work:

    Get-Content list.txt |
    Get-ADComputer -Properties canonicalname,operatingSystem,lastLogonTimestamp |
    select samaccountname,canonicalname,operatingSystem,lastLogonTimestamp

    • Edited by JS2010 Thursday, July 4, 2019 12:11 AM
    Thursday, July 4, 2019 12:11 AM