Remote Desktop
A Microsoft app that connects remotely to computers and to virtual apps and desktops.
3,219 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am a powershell toddler (baby) who is learning by example.
I have a powershell script which inspects each computer currently communicating on my network and lists all local accounts with account information.
The last piece of information needed is password last set. Testing out code to grab this information has me stymied.
$TestComp = "remotecomputer"
$User = "localaccount"
$PasswordLastSet = (Invoke-Command -ComputerName $TestComp -ScriptBlock {
Get-LocalUser localaccount }).PasswordLastSet
write-host $User $PasswordLastSet
outputs the following and is verified correct information
localaccount 2/13/2023 11:09:19 AM
Any fancy powershell magic or mojo to use a variable like in the code below?
$TestComp = "remotecomputer"
$User = "localaccount"
$PasswordLastSet = (Invoke-Command -ComputerName $TestComp -ScriptBlock {
Get-LocalUser $User }).PasswordLastSet
write-host $User $PasswordLastSet
outputs
localaccount
I really do want to get the password last set for a variable list of local accounts on remote computers.
The easiest way is to refer to the variable containing the local account in the scriptblock is to prefix the variable with "$Using:".
Like this:
Get-LocalUser $Using:User }).PasswordLastSet
Download this free PDF: https://www.sapien.com/books_training/Windows-PowerShell-4
Read the 1st half and do all the exercises. Then move on to the 2nd half once you feel comfortable in what you learned in the 1st half. The 2nd half applies what you learned in the 1st half to practical code addressing common administrative tasks.