Home > Blockchain >  How to reformat date from Get-ADComputer output
How to reformat date from Get-ADComputer output

Time:08-12

Using a PowerShell script, how do I reformat the output from "Get-ADComputer -Filter * -Properties Created | FT Name,Created" and then write to reformated output to the computer description in AD.

the current output looks like this below

Name           Created               
----           -------               
LAPTOP12   30/06/2011 10:22:52 AM
LAPTOP03   18/01/2016 3:47:06 PM 
LAPTOP01   12/07/2011 11:04:29 AM
LAPTOP11   30/10/2015 8:27:00 AM 
PC06       11/07/2011 2:03:17 PM 

The format I am looking to create is

computername Provisioned ddMMyyyy

Then write this revised output of "Provisioned ddMMyyy" to the computer description in AD

CodePudding user response:

Get-ADComputer -Filter * -Properties Created | foreach-object { Set-ADComputer $_ -Description "Provisioned $($_.Created.ToString("ddMMyy"))" }
  • No need for ft Name,Created here because ft is for a human-readable output

  • Use the | (pipe) character to send the results of Get-ADComputer to the next command

  • Use foreach-object on the piped output to iterate through each item from the Get-ADComputer output

  • Use Set-ADComputer with the -Description parameter to update the AD description

  • Use the .ToString method to reformat the $_.Created value to your desired format

  • Related