---
title: "Remote Service Logon Accounts: Manage with PowerShell"
description: "Set and manage Service Logon Accounts remotely across multiple computers using PowerShell. A must-read for sysadmins."
canonical: "https://adamtheautomator.com/powershell-service-logon-account/"
---

# Remote Service Logon Accounts: Manage with PowerShell

> Set and manage Service Logon Accounts remotely across multiple computers using PowerShell. A must-read for sysadmins.

Source: https://adamtheautomator.com/powershell-service-logon-account/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Remote Service Logon Accounts: Manage with PowerShell](https://adamtheautomator.com/wp-content/uploads/2019/06/5d238ae5c824b514689ea57a.jpg)

# Remote Service Logon Accounts: Manage with PowerShell

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)29 June 20192 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Active Directory](/tag/active-directory/)[PowerShell](/tag/powershell/)

Table of Contents

*   [The PowerShell Service Logon Account Changer](#the-powershell-service-logon-account-changer)

Using PowerShell, you can easily query many computers at once and set the PowerShell service logon account on all of them! If you need a PowerShell cmd to find and change a service logon account, this post is for you.

A few years ago I was in a position where I was to implement a new Active Directory password policy. All employee user passwords were to begin expiring after 6 months. I had all the 10,000 Active Directory accounts accounted for. I had tagged all user accounts that I thought should be used for services and all that were employees.

Related:[Active Directory Auditing: How to Track Down Password Changes](https://adamtheautomator.com/active-directory-auditing/)

The proper notification was sent out to the entire IT department asking if any employee user accounts were running services on any of the servers. Nope!

The password policy was implemented and the help desk lit up. Why? There were dozens of services on servers running as employee user accounts that had gotten expired! In hindsight, I shouldn’t have taken their word for it and queried all the servers myself but you live you learn. My solution was to throw together this script to set the PowerShell service logon account.

This PowerShell service logon account script loops through all of the services in the CSV, connect to the server in question, changes the service account, stops the service, and then restarts the service to ensure the change is committed. This was written a few years ago so I’m not using [`Get-CimInstance`](https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.2) instead of [`Get-WmiObject`](https://adamtheautomator.com/get-wmiobject/ "Get-WmiObject") among a few other things.

Related:[Get-WmiObject: Querying WMI on Local and Remote Computers](https://adamtheautomator.com/get-wmiobject/)

Although, it should still work great for anyone that may be in a similar position I was.

## The PowerShell Service Logon Account Changer

The below script is a function called `ChangeServiceAccount` which queries a remote computer with WMI and enumerates the service on it matching the `$sServiceName` parameter.

Once the script finds the service, it then calls the `Change()` method on the WMI object which we then pass the username and password for the account we’d like to change.

Since the service must be restarted, it does so using the `StopService()` and `StartService()` methods although you could use `Restart-Service` just as easily.

```powershell
Function ChangeServiceAccount($sServiceName,$sComputerName,$sUsername,$sPassword) {
	$oService = Get-WmiObject -ComputerName $sComputerName -Query "SELECT * FROM Win32_Service WHERE Name = '$sServiceName'"
	$oService.Change($null,$null,$null,$null,$null,$null,"$sUsername",$sPassword) | Out-Null
	$oService.StopService() | Out-Null
	while ($oService.Started) {
		sleep 1
		$oService = Get-WmiObject -ComputerName $sComputerName -Query "SELECT * FROM Win32_Service WHERE Name = '$sServiceName'"
	}##endwhile
	$oService.StartService() | Out-Null
}##endfunction

if (!$sCsvFile) {
	Write-Error 'No CSV file specified' -RecommendedAction 'Please specify CSV file as first parameter'
} elseif (!(Test-Path $sCsvFile)) {
	Write-Error "CSV file '$sCsvFile' not found";
} elseif ((gc $sCsvFile | select -First 1) -ne 'Service,Server,Username,Password') {
	Write-Error "CSV headers are incorrect.  They must be 'Service,Server,Username,Password'"
} else {
	$aRows = Import-Csv $sCsvFile;
	foreach ($oRow in $aRows) {
    	## Set service logon account PowerShell
		changeServiceAccount $oRow.Service $oRow.Server $oRow.Username $oRow.Password
		Write-Host "Changed service account $($oRow.Service) on $($oRow.Server) to $($oRow.Username) and restarted service"
	}##endforeach
}##endif
```

Copy the above PowerShell service logon account script and use it to your heart’s content!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-service-logon-account%2F&text=Remote%20Service%20Logon%20Accounts%3A%20Manage%20with%20PowerShell)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-service-logon-account%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-service-logon-account%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2024/11/image-23.png)

### [Managing Active Directory Groups with PowerShell: The Ultimate Guide](/powershell-ad-groups-guide/)

Learn how to manage Active Directory groups with PowerShell! This hands-on guide shows you how to query, create and modify AD groups using practical real-world

![](https://adamtheautomator.com/wp-content/uploads/2019/08/database-152091_1280.png)

### [Active Directory Database: PowerShell Monitoring Made Easy](/active-directory-database/)

Find the ntds.dit location and monitor your Active Directory database using PowerShell.

![](https://adamtheautomator.com/wp-content/uploads/2019/07/panic-1393619_1280.png)

### [How to Find Locked Out Users in Active Directory with PowerShell](/find-locked-out-users-in-active-directory-powershell/)

See what we can do to find locked out users in Active Directory with PowerShell!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
