win-services
v1.0.0
Published
List, enable, and disable Windows services via PowerShell and sc
Maintainers
Readme
win-services
Node.js library to list, enable, and disable Windows services using PowerShell (Get-CimInstance) and sc.
Requirements
- Windows (
win32) - Node.js 18+
- Administrator privileges for
enableServiceanddisableService
Installation
npm install win-servicesUsage
const {
listServices,
enableService,
disableService,
isWindows,
} = require('win-services');
async function main() {
if (!isWindows()) {
console.error('This library only works on Windows');
return;
}
const services = await listServices();
console.log(services);
// => [{ name: 'Spooler', startName: 'LocalSystem' }, ...]
await disableService('Spooler');
await enableService('Spooler');
}
main().catch(console.error);API
| Function | Description | Command(s) |
|----------|-------------|------------|
| isWindows() | Returns true if the platform is Windows | — |
| listServices() | Lists all services | Get-CimInstance Win32_Service (via PowerShell) |
| disableService(name) | Disables and stops a service | sc config "name" start= disabled + sc stop "name" |
| enableService(name) | Enables and starts a service | sc config "name" start= auto + sc start "name" |
Common errors
| Error | Likely cause |
|-------|--------------|
| This library only works on Windows | Run on macOS/Linux |
| Access is denied | Terminal without Administrator privileges |
| The specified service does not exist | Incorrect service name |
| CommandError | Failed to run PowerShell or sc (see error.code and error.stderr) |
Tests
npm testUnit tests cover the PowerShell output parser. Integration tests must be run manually on a Windows machine with elevated privileges.
License
GPL-3.0
