Emacs: Format PowerShell Code πŸš€

By Xah Lee. Date: .

put this in your Emacs Init File:

(defvar xah-powershell-path nil "A string. Microsoft Windows PowerShell path or name. It can be just a program name such as γ€Œpowershell」 or γ€Œpwsh」, or full path to one of them. Used by many commands that need to launch program via Powershell on Windows.")

(setq xah-powershell-path "pwsh")

(defun xah-pwsh-format-code ()
  "Format PowerShell code of current buffer file.
Buffer must be a file buffer. It is saved first.
Requires xah-format-PowerShell-script at URL `http://xahlee.info/powershell/powershell_format_script.html'

URL `http://xahlee.info/emacs/emacs/emacs_format_powershell_code.html'
Version: 2021-09-13 2022-08-04 2022-08-28"
  (interactive)
  (let ((xfpath (buffer-file-name)))
    (if xfpath
        (let (($cmdstr
               (format "%s -Command xah-format-PowerShell-script %s"
                       xah-powershell-path
                       xfpath)))
          (save-buffer)
          (shell-command $cmdstr)
          (revert-buffer t t t))
      (user-error "%s: current buffer must be a file %s" real-this-command (buffer-name)))))

need PowerShell: Command to Format PowerShell Code

part of Emacs: Xah PowerShell Mode

Emacs, Format Code