hello friends! new(ish)!

Powershell

From InstallGentoo Wiki v2
Revision as of 20:59, 9 June 2015 by >Hevnoraak (uploaded and added PDF ; put in Inital Configuration section)
Jump to navigation Jump to search

Powershell

Windows Powershell is a shell / scripting environment created by Microsoft. You can read more about the technical stuff here. This page serves as not a complete description of Powershell or its history, but a guide on how to use it. Some programming knowledge is expected but not absolutely required. Experience with bash is also probably helpful, but you could learn something even if you have zero experience with any kind of shell or scripting environment.

Rationale

I think Powershell is pretty neat. I used Linux as my main OS for about 5 years and had to switch to Windows very recently for a myriad of reasons. Ironically I might have to switch back soon (taking a *NIX Systems Programming class), but in the meantime I wanted a scripting language for Windows that was better for the everyday grind than Python, so I did some research and found Powershell. Its no bash, but its close, and I think if it was embraced by the world and the Internet (re: people give M$ a reason to care about it) then it could be really great. The fact that Microsoft is actively trying to make Windows a more dev-friendly environment shows that exciting things are happening (See: SSH on Windows), and I believe those changes will start begin with the upcoming changes to Powershell in Windows 10.

- Anon, June 9th 2015

Getting Started =

Here is a chart that I made. File:PsQuickReference-20150609.pdf (I made it as a .pdf because I can't be assed to learn how to use WikiMedia properly, so if you don't like that please contribute yourself.) It was designed to get someone familiar with the *NIX shell up and scripting as fast as possible, and includes all the common commands I remember using often on *NIX.

Let's do a quick analysis of this chart and come up with some pros and cons.

Pros of Powershell:

  • Syntax is sometimes much clearer than bash. Powershell's syntax is also much more consistent. This makes it much easier to compose and read scripts.
  • Syntax is generally not case sensitive. Some people might put this as a con but I like it and I think many others do too.
  • Familiar conventions to get *NIX people started, such as $ for variables, cp, mv, mkdir, etc.
  • Familiar object-oriented conventions are baked in. If you come from a Java, C#, or even Ruby background, lots of things should feel familiar.
  • Decently fast, both performance-wise and to learn.
  • .NET environment, meaning its extensible with your own .NET objects.
  • Lots of juicy default aliases for juicy shortcuts. You can tell whoever wrote this shit actually uses a shell.
  • Access to lots of Windows APIs/features such as the registry and Internet Explorer.
  • Powershell Community Extensions and/or PsGet.
  • Its not cmd.exe ! ! !

Cons of Powershell:

  • Windows only, whereas bash or sh is on basically every OS (including Windows with Cygwin).
  • Cannot run scripts by default, and Powershell only comes by default with Win7 and up. Makes scripts unportable because they are not really guarunteed to run on another machine.
  • Sometimes the commands are quite verbose. It can be especially annoying to type out very verbose arguments, such as '-Recurse' as opposed to '-r'.
  • Fairly slow autocomplete (hopefully will be fixed in Windows 10).
  • No reverse i-search by default.
  • Capitals everywhere. Why? I know it usually doesn't matter but its still kind of ugly.
  • Binary logs (Get-History is an example, I think).

Initial Configuration

First thing you're gonna wanna do is fix Microsoft's idiocy and make scripts executable. To do that, run this command:

   Set-ExecutionPolicy -CurrentUser RemoteSigned

You might need to do it in an elevated prompt. To do that, open the Start Menu, type 'Windows Powershell', right-click the link, and select 'Run as Administrator'. Then enter the command.

Next, you'll probably want a config file to store custom aliases, functions, and the like. To do this, run this little snippet:

   if ( -Not (Test-Path $profile)) {
       New-Item -Type File $profile
   }
   Notepad $profile

This will be where you store your customized Powershell configuration.

Some Helpful Default Aliases

Getting Help

Ricer Bullshit

Scripting Stuff

Loops

Conditionals

Variables and Data Types

Working with Data

Format Commands

Tips and Tricks

Working with Windows

References