If you enjoyed this site, please consider donating $3. Any amount is appreciated. Thanks!

Emacs PowerShell Modes

Xah Lee, 2009-07

There are 2 emacs modes for powershell. One is powershell-mode, that does syntax coloring for your PowerShell scripts. The other lets you run powershell interactively within emacs instead of Windows Console.

powershell-mode for PowerShell Scripts

To get a PowerShell mode for emacs, get powershell-mode.el by Vivek Sharma: http://www.viveksharma.com/techlog/attache/powershell-mode.el.txt. This lets you edit PowerShell scripts with syntax highlighting etc.

Note: as of version 0.5 of powershell-mode, it has a bug that generats the error “(wrong-type-argument stringp t)”. This error is quite harmless but you can get rid of it by commenting out the devar of powershell-imenu-expressions (line 167-170), and comment out the defun of powershell-setup-imenu (line 171-179), and comment out line 206 of “(powershell-setup-imenu)”.

Install

After download, open the file, then type “Alt+x eval-buffer”. Now, open any PowerShell source code file, then type “Alt+x powershell-mode”, you'll see the source code syntax colored.

To have emacs automatically load and use the mode when opening files ending in “.ps1”, put the following lines in your emacs init file (usually “~/.emacs”):

;; add dir to load path
(add-to-list 'load-path "/path/to/parent/dir/") ;; make sure dir is the file's parent

;; powershell-mode
(autoload 'powershell-mode "powershell-mode" "A editing mode for Microsoft PowerShell." t)
(add-to-list 'auto-mode-alist '("\\.ps1\\'" . powershell-mode)) ; PowerShell script

PowerShell Interactive Shell In Emacs

To setup emacs to run PowerShell as a interactive shell in emacs, install the elisp file by Dino Chiesa, at: http://blogs.msdn.com/dotnetinterop/archive/2008/04/10/run-powershell-as-a-shell-within-emacs.aspx.

Install

After download, open the file, then type “Alt+x eval-buffer”. Now, you can type “Alt+x powershell”, then you'll have a interactive powershell in emacs.

To have emacs automatically have the command “powershell” available, and load it when you call it, put the following lines in your emacs init file (usually “~/.emacs”):

;; add the dir to load path
(add-to-list 'load-path "/path/to/parent/dir/") ;; make sure dir is the file's parent

;; autoload powershell interactive shell
(autoload 'powershell "powershell" "Start a interactive shell of PowerShell." t)

Related essays:

2009-07
© 2009 by Xah Lee.