Skip to content

Conversation

@afnanashraf10
Copy link

@afnanashraf10 afnanashraf10 commented Oct 19, 2025

Description

Adds automatic shell detection for Windows containers to support both ServerCore and NanoServer.

Problem

k9s currently hardcodes powershell for all Windows containers, which fails on NanoServer images that only include cmd.exe.

Error logs:

2025-10-19T21:47:11+04:00 ERR Exec failed error="exit status 1" 
cmd="[/usr/local/bin/kubectl exec ... -- powershell]"

Solution

Instead of hardcoding PowerShell, the fix uses shell detection:

cmd /c where powershell >nul 2>&1 && powershell || cmd

This command:

  1. Checks if PowerShell exists using where powershell
  2. Redirects output to avoid cluttering the console (>nul 2>&1)
  3. Uses PowerShell if found (ServerCore) via &&
  4. Falls back to cmd if not found (NanoServer) via ||

Benefits

  • ✅ Maintains PowerShell preference for ServerCore (better shell features)
  • ✅ Automatically works with NanoServer containers
  • ✅ No breaking changes for existing users

Testing

  • Tested with Windows ServerCore container - uses PowerShell
  • Tested with Windows NanoServer container - uses cmd
  • Verified shell access works in both scenarios

References

Fixes #1929

Windows NanoServer containers don't have PowerShell installed,
only cmd.exe. This change adds automatic detection to use
PowerShell when available (ServerCore) and fallback to cmd
when not (NanoServer).

The solution uses: cmd /c where powershell >nul 2>&1 && powershell || cmd
- Checks if powershell.exe exists in PATH
- Uses PowerShell if available (better shell experience)
- Falls back to cmd if PowerShell is not found

Fixes derailed#1929
@afnanashraf10
Copy link
Author

@derailed Would you be able to review this PR when you get a chance?

This fix addresses issue #1929 by implementing automatic shell detection for Windows containers to support both ServerCore (PowerShell) and NanoServer (cmd.exe) images, rather than hardcoding PowerShell which fails on NanoServer.

The solution uses a simple shell detection command that checks for PowerShell availability and falls back to cmd.exe if needed. I've tested it with both Windows container types and it's working as expected.

Thanks for maintaining k9s! 🐶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Access a Windows Pod

1 participant