Windows PowerShell Cookbook: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Cookbook==
[[Category:Powershell]] [[Category:Windows]]
Download content from the web. This will print out the markup from the index page:
== Fetching remote content ==
 
=== Download content from the web ===
 
This will print out the markup from the index page (which could then be piped through additional commands):
 
<syntaxhighlight lang="powershell">
<syntaxhighlight lang="powershell">
(new-object Net.WebClient).DownloadString("http://damienjay.com/")
> (new-object Net.WebClient).DownloadString("http://damienjay.com/")
</syntaxhighlight>
</syntaxhighlight>
[[Category:Web Development]]
 
== Grep/Searching the content of files ==
 
=== Files in a single directory ===
 
<syntaxhighlight lang="powershell">
> select-string .\*.* -pattern "\my_regexp\"
</syntaxhighlight>
 
=== Recursive search ===
 
<syntaxhighlight lang="powershell">
> gci path\to\search\root\ -rec | select-string -pattern "\my_regexp\"
</syntaxhighlight>
 
=== Recursive search filtered by file type ===
 
<syntaxhighlight lang="powershell">
> Get-ChildItem path\to\search\root\ -include *.txt -rec | select-string -pattern "\my_regexp\"
> # or, using aliases for the commands and not using "-include" ...
> gci path\to\search\root\ *.txt -r | sls -pattern "\my_regexp\"
</syntaxhighlight>
 
== List directory contents ==
 
[http://technet.microsoft.com/en-us/library/ee692796.aspx Fun Things You Can Do With the Get-ChildItem Cmdlet] (Microsoft TechNet)
 
<syntaxhighlight lang="powershell">
> Get-ChildItem .\
> # or...
> gci .\
> # or...
> gci # for the current directory
</syntaxhighlight>
 
=== Limit listing to file names ===
 
<syntaxhighlight lang="powershell">
> gci path\to\directory | Select-Object Name
> # or...
> Get-ChildItem path\to\directory -name
</syntaxhighlight>
 
=== Recursive listing ===
 
<syntaxhighlight lang="powershell">
> gci -recursive
> # or...
> gci -rec
</syntaxhighlight>
 
== Environment variables ==
 
Displaying, creating, and modifying environment variables using Powershell CLI.<ref>[https://technet.microsoft.com/en-us/library/ff730964.aspx Creating and Modifying Environment Variables] - Microsoft TechNet</ref>
 
See also [[Powershell Environment Configuration]].
 
=== List all environment variables ===
 
<syntaxhighlight lang="powershell">
> Get-ChildItem Env:
> # (or using gci alias...)
> gci env:
</syntaxhighlight>
 
=== Display value of a single environment variable ===
 
`$Env:` followed by the variable name, e.g.:
 
<syntaxhighlight lang="powershell">
> $Env:OS
</syntaxhighlight>
 
=== Creating and modifying environment variables ===
 
==== Creating a process-level environment variable ====
 
<syntaxhighlight lang="powershell">
> $env:TestVariable = "This is the test variable value."
</syntaxhighlight>
 
==== Creating a permanent environment variable ====
 
<syntaxhighlight lang="powershell">
> [Environment]::SetEnvironmentVariable("TestVariable", "Test value.", "User")
</syntaxhighlight>
 
<div class="alert alert-warning">N.B. It's necessary to create a new Powershell instance to refer to new environment variable values.</div>
 
==== Deleting an environment variable ====
 
<syntaxhighlight lang="powershell">
> Remove-Item Env:TestVariable
> # (or...)
> [Environment]::SetEnvironmentVariable("TestVariable", $null, "User")
</syntaxhighlight>
 
== Notes ==
<references />

Latest revision as of 19:14, 30 November 2015

Fetching remote content[edit]

Download content from the web[edit]

This will print out the markup from the index page (which could then be piped through additional commands):

> (new-object Net.WebClient).DownloadString("http://damienjay.com/")

Grep/Searching the content of files[edit]

Files in a single directory[edit]

> select-string .\*.* -pattern "\my_regexp\"

Recursive search[edit]

> gci path\to\search\root\ -rec | select-string -pattern "\my_regexp\"

Recursive search filtered by file type[edit]

> Get-ChildItem path\to\search\root\ -include *.txt -rec | select-string -pattern "\my_regexp\"
> # or, using aliases for the commands and not using "-include" ...
> gci path\to\search\root\ *.txt -r | sls -pattern "\my_regexp\"

List directory contents[edit]

Fun Things You Can Do With the Get-ChildItem Cmdlet (Microsoft TechNet)

> Get-ChildItem .\
> # or...
> gci .\
> # or...
> gci # for the current directory

Limit listing to file names[edit]

> gci path\to\directory | Select-Object Name
> # or...
> Get-ChildItem path\to\directory -name

Recursive listing[edit]

> gci -recursive
> # or...
> gci -rec

Environment variables[edit]

Displaying, creating, and modifying environment variables using Powershell CLI.[1]

See also Powershell Environment Configuration.

List all environment variables[edit]

> Get-ChildItem Env:
> # (or using gci alias...)
> gci env:

Display value of a single environment variable[edit]

$Env: followed by the variable name, e.g.:

> $Env:OS

Creating and modifying environment variables[edit]

Creating a process-level environment variable[edit]

> $env:TestVariable = "This is the test variable value."

Creating a permanent environment variable[edit]

> [Environment]::SetEnvironmentVariable("TestVariable", "Test value.", "User")
N.B. It's necessary to create a new Powershell instance to refer to new environment variable values.

Deleting an environment variable[edit]

> Remove-Item Env:TestVariable
> # (or...)
> [Environment]::SetEnvironmentVariable("TestVariable", $null, "User")

Notes[edit]