Last password change in macOS Objective is to determine the last password change time for all the user accounts in macOS (Catalina).

sudo dscl . -read Users/swastibhushandeb accountPolicyData


Output:

&lt;dict&gt;
    &lt;key&gt;creationTime&lt;/key&gt;
    &lt;real&gt;1570887333.512722&lt;/real&gt;
    &lt;key&gt;failedLoginCount&lt;/key&gt;
    &lt;integer&gt;0&lt;/integer&gt;
    &lt;key&gt;failedLoginTimestamp&lt;/key&gt;
    &lt;integer&gt;0&lt;/integer&gt;
    &lt;key&gt;passwordLastSetTime&lt;/key&gt;
    &lt;real&gt;1570887334.500102&lt;/real&gt;
&lt;/dict&gt;
&lt;/plist&gt;


date -r can be used to convert the epoch time stamps to human readable format as:


date -r 1570887334

How can the above be wrapped in a  bash script/one liner code?Thanks in advance.
date -r $(sudo dscl . -read /Users/username accountPolicyData |
  tail -n +2 |
  plutil -extract passwordLastSetTime xml1 -o - -- - |
  sed -n &quot;s/&lt;real&gt;\([0-9]*\).*/\1/p&quot;)


date -r &lt;seconds&gt; takes some seconds.
$(…) runs a command and captures stdout.

sudo dscl . -read /Users/… accountPolicyData returns accountPolicyData as XML.
tail -n +2 drops the first line of the output of the previous command, because dscl prints some extra information before the plist starts.
plutil -extract passwordLastSetTime xml1 -o - -- -

-extract passwordLastSetTime xml1 extracts the value for the key passwordLastSetTime in the input, outputting as xml
-o - outputs to stdout
-- - reads from stdin

-- to stop parsing options
- for stdin




sed -n &quot;s/&lt;real&gt;\([0-9]*\).*/\1/p&quot;) extracts just the integer from the output.

-n suppresses the default of printing every input line
s/a/b/p substitutes a for b, printing matching lines (overriding -n for individual lines)

&lt;real&gt; matches the start of the line containing the integer we want
\(…\) is a capturing group

[0-9]* matches digits any number of times


.* matches to the end of the line, thus removing content in the line we're not interested in
\1 is the first capturing group




| pipes stdout from the left command into stdin of the right command



You can put this command all on one line if you prefer.