execute_ps
Execute PowerShell commands directly on Windows systems. Use the tool to run scripts, manage modules, and retrieve system information efficiently via the PowerShell MCP Server.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"command": "<command>"
},
"name": "execute_ps"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | PowerShell command to execute |
Implementation Reference
- src/index.ts:39-76 (handler)Handler function that executes the provided PowerShell command using Node.js child_process execAsync. Escapes double quotes in the command, captures stdout and stderr, and returns formatted text content or error response.
async ({ command }: { command: string }) => { try { const { stdout, stderr } = await execAsync( `powershell -Command "${command.replace(/"/g, '\\"')}"` ); if (stderr) { return { isError: true, content: [ { type: 'text' as const, text: `Error executing PowerShell command: ${stderr}`, }, ], }; } return { content: [ { type: 'text' as const, text: stdout, }, ], }; } catch (error) { return { isError: true, content: [ { type: 'text' as const, text: `Error executing PowerShell command: ${(error as Error).message}`, }, ], }; } } - src/index.ts:36-38 (schema)Zod schema defining the input parameter 'command' as a string for the execute_ps tool.
{ command: z.string().describe('PowerShell command to execute'), }, - src/index.ts:34-77 (registration)Registration of the 'execute_ps' tool with the MCP server using this.server.tool, including inline schema and handler.
this.server.tool( 'execute_ps', { command: z.string().describe('PowerShell command to execute'), }, async ({ command }: { command: string }) => { try { const { stdout, stderr } = await execAsync( `powershell -Command "${command.replace(/"/g, '\\"')}"` ); if (stderr) { return { isError: true, content: [ { type: 'text' as const, text: `Error executing PowerShell command: ${stderr}`, }, ], }; } return { content: [ { type: 'text' as const, text: stdout, }, ], }; } catch (error) { return { isError: true, content: [ { type: 'text' as const, text: `Error executing PowerShell command: ${(error as Error).message}`, }, ], }; } } );