We are still actively working on the spam issue.
Difference between revisions of "Powershell"
m (uploaded and added PDF ; put in Inital Configuration section) |
m (added some stuff) |
||
Line 68: | Line 68: | ||
=== Some Helpful Default Aliases === | === Some Helpful Default Aliases === | ||
+ | |||
+ | Here are some useful aliases that are baked into Powershell. You'll | ||
+ | probably find yourself using them often, and they may seem familiar, | ||
+ | so take a good, long look. | ||
+ | |||
+ | CommandType Name | ||
+ | ----------- ---- | ||
+ | Alias % -> ForEach-Object | ||
+ | Alias % -> ForEach-Object | ||
+ | Alias ? -> Where-Object | ||
+ | Alias h -> Get-History | ||
+ | Alias r -> Invoke-History | ||
+ | Alias ac -> Add-Content | ||
+ | Alias cd -> Set-Location | ||
+ | Alias compare -> Compare-Object | ||
+ | Alias cp -> Copy-Item | ||
+ | Alias curl -> Invoke-WebRequest | ||
+ | Alias diff -> Compare-Object | ||
+ | Alias foreach -> ForEach-Object | ||
+ | Alias ft -> Format-Table | ||
+ | Alias fw -> Format-Wide | ||
+ | Alias gc -> Get-Content | ||
+ | Alias kill -> Stop-Process | ||
+ | Alias ls -> Get-ChildItem | ||
+ | Alias man -> help | ||
+ | Alias measure -> Measure-Object | ||
+ | Alias nal -> New-Alias | ||
+ | Alias nv -> New-Variable | ||
+ | Alias ps -> Get-Process | ||
+ | Alias pwd -> Get-Location | ||
+ | Alias r -> Invoke-History | ||
+ | Alias ren -> Rename-Item | ||
+ | Alias rm -> Remove-Item | ||
+ | Alias rmdir -> Remove-Item | ||
+ | Alias rv -> Remove-Variable | ||
+ | Alias sc -> Set-Content | ||
+ | Alias select -> Select-Object | ||
+ | Alias sort -> Sort-Object | ||
+ | Alias start -> Start-Process | ||
+ | Alias sv -> Set-Variable | ||
+ | Alias tee -> Tee-Object | ||
+ | Alias type -> Get-Content | ||
+ | Alias wget -> Invoke-WebRequest | ||
+ | Alias where -> Where-Object | ||
+ | |||
=== Getting Help === | === Getting Help === | ||
Line 84: | Line 129: | ||
=== Format Commands === | === Format Commands === | ||
+ | Powershell provides several commands for changing the way that it displays data within the shell. | ||
+ | |||
+ | PS C:\> get-command | ? { $_.Verb -eq "Format" -and $_.ModuleName -eq "Microsoft.Powershell.Utility" } | ||
+ | |||
+ | CommandType Name ModuleName | ||
+ | ----------- ---- ---------- | ||
+ | Cmdlet Format-Custom Microsoft.PowerShell.Utility | ||
+ | Cmdlet Format-List Microsoft.PowerShell.Utility | ||
+ | Cmdlet Format-Table Microsoft.PowerShell.Utility | ||
+ | Cmdlet Format-Wide Microsoft.PowerShell.Utility | ||
+ | |||
+ | ==== Format-Wide ===== | ||
+ | |||
+ | The most boring one: it simply puts the data in multiple columns to make better use of horizontal screen space, like so: | ||
+ | |||
+ | PS C:\> get-process | ||
+ | |||
+ | Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName | ||
+ | ------- ------ ----- ----- ----- ------ -- ----------- | ||
+ | 82 7 1076 4216 44 1672 armsvc | ||
+ | 135 9 5800 8480 39 0.09 5320 audiodg | ||
+ | 316 25 15748 23808 200 6.48 2776 ConEmu | ||
+ | 130 11 2952 6520 91 7.09 3660 ConEmuC64 | ||
+ | 51 6 964 4008 49 0.02 4936 conhost | ||
+ | 55 7 1404 4628 50 7.23 5828 conhost | ||
+ | 212 12 1792 3756 44 524 csrss | ||
+ | 180 16 2428 38256 202 600 csrss | ||
+ | 314 17 3880 10912 44 1728 dasHost | ||
+ | ... | ||
+ | |||
+ | PS C:\> get-process | format-wide | ||
+ | |||
+ | armsvc ConEmu | ||
+ | ConEmuC64 conhost | ||
+ | conhost csrss | ||
+ | csrss dasHost | ||
+ | .... | ||
+ | |||
+ | |||
+ | Note that this seems to kill your use of Select-Object: | ||
+ | |||
+ | PS C:\> ps | select-object -Property Name,ID,Handles | ||
+ | |||
+ | Name Id Handles | ||
+ | ---- -- ------- | ||
+ | armsvc 1672 82 | ||
+ | audiodg 5320 126 | ||
+ | ... | ||
+ | |||
+ | PS C:\> ps | select-object -Property Name,ID,Handles | fw | ||
+ | |||
+ | armsvc ConEmu | ||
+ | ConEmuC64 conhost | ||
+ | conhost csrss | ||
+ | csrss dasHost | ||
+ | ... | ||
+ | |||
+ | ==== Format-List ==== | ||
+ | |||
+ | Format-List takes an object or an array of objects and gives a | ||
+ | descriptive output of the object's (or objects') properties and values | ||
+ | associated with said properties. To be honest, Format-List is kind of | ||
+ | a crappy name; the output doesn't really look like a list to me, more | ||
+ | like a table. This command is similar to Get-Member but doesn't | ||
+ | produce as much output and gives you the data instead of just what | ||
+ | data members the object has. | ||
+ | |||
+ | PS C:\> get-date | ||
+ | |||
+ | Tuesday, June 9, 2015 8:58:23 PM | ||
+ | |||
+ | PS C:\> get-date | format-list | ||
+ | |||
+ | DisplayHint : DateTime | ||
+ | Date : 6/9/2015 12:00:00 AM | ||
+ | Day : 9 | ||
+ | DayOfWeek : Tuesday | ||
+ | DayOfYear : 160 | ||
+ | Hour : 20 | ||
+ | Kind : Local | ||
+ | Millisecond : 922 | ||
+ | Minute : 58 | ||
+ | Month : 6 | ||
+ | Second : 27 | ||
+ | Ticks : 635694803079228445 | ||
+ | TimeOfDay : 20:58:27.9228445 | ||
+ | Year : 2015 | ||
+ | DateTime : Tuesday, June 9, 2015 8:58:27 PM | ||
=== Tips and Tricks === | === Tips and Tricks === |
Revision as of 02:07, 10 June 2015
Contents
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
Here are some useful aliases that are baked into Powershell. You'll probably find yourself using them often, and they may seem familiar, so take a good, long look.
CommandType Name ----------- ---- Alias % -> ForEach-Object Alias % -> ForEach-Object Alias ? -> Where-Object Alias h -> Get-History Alias r -> Invoke-History Alias ac -> Add-Content Alias cd -> Set-Location Alias compare -> Compare-Object Alias cp -> Copy-Item Alias curl -> Invoke-WebRequest Alias diff -> Compare-Object Alias foreach -> ForEach-Object Alias ft -> Format-Table Alias fw -> Format-Wide Alias gc -> Get-Content Alias kill -> Stop-Process Alias ls -> Get-ChildItem Alias man -> help Alias measure -> Measure-Object Alias nal -> New-Alias Alias nv -> New-Variable Alias ps -> Get-Process Alias pwd -> Get-Location Alias r -> Invoke-History Alias ren -> Rename-Item Alias rm -> Remove-Item Alias rmdir -> Remove-Item Alias rv -> Remove-Variable Alias sc -> Set-Content Alias select -> Select-Object Alias sort -> Sort-Object Alias start -> Start-Process Alias sv -> Set-Variable Alias tee -> Tee-Object Alias type -> Get-Content Alias wget -> Invoke-WebRequest Alias where -> Where-Object
Getting Help
Ricer Bullshit
Scripting Stuff
Loops
Conditionals
Variables and Data Types
Working with Data
Format Commands
Powershell provides several commands for changing the way that it displays data within the shell.
PS C:\> get-command | ? { $_.Verb -eq "Format" -and $_.ModuleName -eq "Microsoft.Powershell.Utility" } CommandType Name ModuleName ----------- ---- ---------- Cmdlet Format-Custom Microsoft.PowerShell.Utility Cmdlet Format-List Microsoft.PowerShell.Utility Cmdlet Format-Table Microsoft.PowerShell.Utility Cmdlet Format-Wide Microsoft.PowerShell.Utility
Format-Wide =
The most boring one: it simply puts the data in multiple columns to make better use of horizontal screen space, like so:
PS C:\> get-process Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 82 7 1076 4216 44 1672 armsvc 135 9 5800 8480 39 0.09 5320 audiodg 316 25 15748 23808 200 6.48 2776 ConEmu 130 11 2952 6520 91 7.09 3660 ConEmuC64 51 6 964 4008 49 0.02 4936 conhost 55 7 1404 4628 50 7.23 5828 conhost 212 12 1792 3756 44 524 csrss 180 16 2428 38256 202 600 csrss 314 17 3880 10912 44 1728 dasHost ...
PS C:\> get-process | format-wide armsvc ConEmu ConEmuC64 conhost conhost csrss csrss dasHost ....
Note that this seems to kill your use of Select-Object:
PS C:\> ps | select-object -Property Name,ID,Handles
Name Id Handles ---- -- ------- armsvc 1672 82 audiodg 5320 126 ...
PS C:\> ps | select-object -Property Name,ID,Handles | fw armsvc ConEmu ConEmuC64 conhost conhost csrss csrss dasHost ...
Format-List
Format-List takes an object or an array of objects and gives a descriptive output of the object's (or objects') properties and values associated with said properties. To be honest, Format-List is kind of a crappy name; the output doesn't really look like a list to me, more like a table. This command is similar to Get-Member but doesn't produce as much output and gives you the data instead of just what data members the object has.
PS C:\> get-date Tuesday, June 9, 2015 8:58:23 PM PS C:\> get-date | format-list DisplayHint : DateTime Date : 6/9/2015 12:00:00 AM Day : 9 DayOfWeek : Tuesday DayOfYear : 160 Hour : 20 Kind : Local Millisecond : 922 Minute : 58 Month : 6 Second : 27 Ticks : 635694803079228445 TimeOfDay : 20:58:27.9228445 Year : 2015 DateTime : Tuesday, June 9, 2015 8:58:27 PM