惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - Coding让生活更美好

Entity Framework search sequnce Runtime Complexity of .NET Generic Collection update the UI property cross thread 打通技术的任督二脉 windbg symbol path 一个文件夹可以link 到另外一个文件夹 Series on Coded UI Test Extensibility Launching the Debugger Automatically 修复软件安装问题和软件卸载问题 How to do code coverage test for windows service EF6 大数据框架整理 技术要点整理 修改table 设置默认值 To open the project created by previous version of Visual Studio 获取Powershell write-host 的内容 Powershell 转义字符 asp.net Login控件基本属性及事件说明 if test project can't be opened in devenv
PowerShell - Special Characters And Tokens
Coding让生活更美好 · 2012-10-26 · via 博客园 - Coding让生活更美好

$ (dollar sign)
Declare/use a variable
Example.

$abc = "123"

$_ (dollar underscore)


'THIS' token. Typically refers to the item inside a foreach loop.
Task: Print all items in a collection.
Solution.

... | foreach { Write-Host $_ }

$$ (double dollar, two dollars) 
Last entered token. Does NOT refer to a whole command.
Task: Say hello world twice.
Solution.

Write-Host "Hello, world!"
$$

$? (dollar sign + question mark) 
Many google searches were looking for this information, so I experimentally found what it does.

Returns True or False value indicating whether previous command ended with an error. For some reason it does not catch all errors, but most of the time it works. 
Task 1: See if a powershell cmdlet exists in the system.
Code.

SomeCmdLet #does not exists
$?

$?

Output.

The term 'SomeCmdLet' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the

spelling of the name, or if a path was included, verify that the path is correct and try again.

At line:1 char:15

+     SomeCmdLet <<<<  #does not exists

    + CategoryInfo          : ObjectNotFound: (SomeCmdLet:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

False    #error occured - previous cmdlet (SomeCmdLet) was not found

True     #no errors returned by the previous command ($?)

Task 2: See if a WMI class exists in the system

Code.

gwmi win32_processo -ErrorAction SilentlyContinue   #intentional error, win32_processor is the right one

$?

$?

Output.

| (pipeline)
Catch output of the command and pass it to another command.
Task: Get list of processes and select top 3 items.
Solution.

Get-Process | Select-Object -first 3

% (percentage) 
1. Shortcut to foreach.
Task: Print all items in a collection.
Solution.

... | % { Write-Host $_ }

2. Same as Mod in VB.
Example:

5 % 2

.. (double dot) 
Specify a range.
Task: Print numbers 1 through 5 without a foreach loop.
Code.

1..5

Output.

1
2
3
4
5

:: (double-colon) 
Thanks to Darrell for asking about this one.

Reference static member of a class.

Task: Compare two strings.
Code.

[string]::Equals("a", "b")

Output.

False

! (exclamation mark)
Thanks to Leo for asking about this one. 
Shortcut to -not.
Code.

$a = $null;
if(!$a) { Write-Host '$a is null' }

Output.

$a is null

? (question mark)
Output all items that conform with condition (shortcut to where). Shortcut to the following: 

foreach { if (...) {return ... } }

Task: Print all odd numbers between 1 and 5 (inclusive):
Code.

1..5 | ? { $_ % 2 }

Output.

1
3
5

` (backtick)
1. Continue command on the next line.
Code.

Write-Host `
"Hello, world!"

Output.

Hello, world!

2. Include a special symbol into a string. Available options:
`0 - Null. My preference is using $null instead.
`a - Alert. Yes, it does make sound, and you can use multiple for multiple beeps.
`b - Backspace
`f - form feed - only affects printed documents.
`n - New line
`r - Carriage return
`t - Horizontal tab
`v - Vertical tab - only affects printed documents.
`' - Single quote - I prefer using double quotes when I need to escape a single one, since I don't need any escaping in this case.
`" - Double quote - you can use single quotes, and you don't need this symbol. My preference is use standard escaping instead, so 4 double quotes ("""") means a double quote.
Official article by Microsoft with full description on every token: 
http://technet.microsoft.com/en-us/library/hh847835.aspx

# (pound sign)
Single line comment.
Example.

#This is a commented line
#This is a second one

<# ... #> (less triangle / lt + pound ... pound + greater triangle / gt)

Block/Multi-line comment.
Example.

<#This is
a commented
block#>

& (ampersand)
Execute string as command.
Example.

& "Get-Process"


@( ) (email-at + round brackets)
Declare arrays.

Note: comma is used as a separator, in contrast to hash table declaration.
Example.

$a = @("One", "Two", "Three")

@{ } (email-at + curly brackets/braces)
Declare hash tables.

Note: semicolon is used as a separator, in contrast to array declaration.

Example.  

$a = @{"1" = "one"; "2" = "two"; "3" = "three"}

@' ... '@ (email-at + single quote ... single quote + email-at)
Here-string without embedded variables.
Code.

@'
$(1+2)
$(3+4)
$(5+6) 
'@

Output.

$(1+2)
$(3+4)
$(5+6)

@" ... "@ (email-at + double quote ... double quote + email-at)
Here-string with embedded variables.
Code.

@"
$(1+2)
$(3+4)
$(5+6)
"@

Output.

3
7
11