r/PowerShell • • 18h ago

News Powershell 7 is coming to Windows

Thumbnail devblogs.microsoft.com
251 Upvotes

r/PowerShell • • 19h ago

Question Byte (Array) to Integer

8 Upvotes

Good evening,

I'm currently dabbling in ASN1 Encoding and I've run into an annoying issue. It seems I was lucky enough to somehow never have encountered this before:

How can I convert from a byte array to integer without writings logic for all the different cases that can happen

[BitConverter] ::ToInt32() needs at least a byte array with the size of 4. Furthermore, the other way around it will always at least spit out a byte array with size 4 even if the int is in the singlebyte range with the rest zeroed (this is especially annoying in conjunction with my ASN1 practice)

[Convert]::ToInt32 only works with single items but not arrays

ToByte Method needs a format which I can't seem to find anywhere. Google search is pretty much Full of the two convert Classes or some generic IFormat stuff but no list what the formats for the methods are

Is there anything that will give me an int (non array) if the byte array im converting from has only one Value (and vice versa)

Ah an while I'm at it:

Is there a way to set the default output type when doing bit manipulation?

I im at the point where I get more than mildly annoyed if I so much as read "significant bit" anywhere. Gimme unsigned


r/PowerShell • • 6h ago

Question PowerShell rules for AI coding agent

0 Upvotes

We know that the default shell for AI coding agents is Bash/Python, but for people like me who use agents on Windows without WSL, I prefer to use PowerShell.

So I wrote these rules for AI coding agents to use PowerShell instead of Bash.

Do you think these rules are OK, or is there anything I might have missed?

# Global Rules

## Shell: PowerShell 7.x (`pwsh`) only

- ALWAYS set `shell_flavor: "powershell"` on `exec` (default is bash). Never use bash/sh/WSL/cmd; never nest shells (`bash -c`, `pwsh -Command`).
- Write PowerShell syntax. Use `workdir` instead of `cd ... &&`. Quote paths with spaces.
- Prefer native cmdlets (`Invoke-RestMethod`, `ConvertFrom-Json`, `Where-Object`, `Select-String`, `Get-ChildItem`, `Test-Path`, `Get-Content`) over `curl`, `jq`, `grep`, `ls`, `cat`, `test`.
- External tools (`git`, `mvn`, `docker`, `npm`) are fine; check `$LASTEXITCODE`.
- Bash → PowerShell: `VAR=x cmd` → `$env:VAR='x'; cmd`; `export` → `$env:`; heredoc → `@'...'@`; `/dev/null` → `$null`; escape char is backtick; single quotes = literal. Quote native args containing `,` or `.` (e.g. `"-Dtest=A,B"`).
- Complex logic: write a `.ps1` (`#Requires -Version 7.0`), never `.sh`.
- Use splatting for many parameters, `Write-Output` (not `Write-Host`), `-Encoding utf8` when writing files, `-LiteralPath` for paths with `[ ]`.
- Confirm with the user before destructive commands (`Remove-Item -Recurse -Force`); preview with `-WhatIf`.

## Token Economy

- Use dedicated `grep`, `find_file_by_name`, `read`, `edit` tools instead of shell for search/read/edit.
- Limit output: `Select-Object -First N`, `Get-Content -Tail N`, `Select-String -Pattern ... -List`/`-Context 0`. Never dump whole files or logs to the shell.
- Return only needed fields: `Select-Object Name,Length`, not full objects. Use `Format-List`/`Format-Table -AutoSize` only at the end and only when needed; avoid wide tables.
- Build/test output (`mvn`, `npm`, `docker`): add quiet flags (`mvn -q -B`, `--no-transfer-progress`, `npm --silent`), then show only failures/summary, e.g. `... 2>&1 | Select-String 'ERROR|FAIL|Tests run:' | Select-Object -Last 30`.
- Save long output to a temp file and read only the relevant lines; do not re-read files already read this session.
- Batch independent commands in one call; chain dependent ones with `&&` or `;`.
- Use `git diff --stat`, `git log --oneline -n 10`, `git status -sb` before full diffs.
- Be concise in replies: no restating the task, no summaries of unchanged code, no unrequested explanations.