Windows PowerShell Cookbook: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
==Cookbook==
== Cookbook ==
Download content from the web. This will print out the markup from the index page (which could then be piped through additional commands):
 
=== 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>
=== Grep/Searching the content of files ===
Files in a single directory:
<syntaxhighlight lang="powershell">
> select-string .\*.* -pattern "\my_regexp\"
</syntaxhighlight>
Recursively:
<syntaxhighlight lang="powershell">
> gci path\to\search\root\ -rec | select-string -pattern "\my_regexp\"
</syntaxhighlight>
Recursively, filtering 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>
Just file names:
<syntaxhighlight lang="powershell">
> gci path\to\directory | Select-Object Name
> # or...
> Get-ChildItem path\to\directory -name
</syntaxhighlight>
Recursive:
<syntaxhighlight lang="powershell">
> gci -recursive
> # or...
> gci -rec
</syntaxhighlight>
[[Category:Web Development]]
[[Category:Web Development]]

Revision as of 18:08, 30 July 2014

Cookbook

Download content from the web

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

Files in a single directory:

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

Recursively:

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

Recursively, filtering by file type:

> 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

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

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

Just file names:

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

Recursive:

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