Windows PowerShell Cookbook: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
m (Video8 moved page Using Windows PowerShell to Windows PowerShell Cookbook: Use naming convention consistent with other articles)
No edit summary
Line 1: Line 1:
== Cookbook ==
[[Category:Powershell]] [[Category:Windows]]
 
== Download content from the web ==
=== Download content from the web ===


This will print out the markup from the index page (which could then be piped through additional commands):
This will print out the markup from the index page (which could then be piped through additional commands):
Line 9: Line 8:
</syntaxhighlight>
</syntaxhighlight>


=== Grep/Searching the content of files ===
== Grep/Searching the content of files ==


Files in a single directory:
=== Files in a single directory ===


<syntaxhighlight lang="powershell">
<syntaxhighlight lang="powershell">
Line 17: Line 16:
</syntaxhighlight>
</syntaxhighlight>


Recursively:
=== Recursive search ===


<syntaxhighlight lang="powershell">
<syntaxhighlight lang="powershell">
Line 23: Line 22:
</syntaxhighlight>
</syntaxhighlight>


Recursively, filtering by file type:
=== Recursive search filtered by file type ===


<syntaxhighlight lang="powershell">
<syntaxhighlight lang="powershell">
Line 31: Line 30:
</syntaxhighlight>
</syntaxhighlight>


=== List directory contents ===
== List directory contents ==


[http://technet.microsoft.com/en-us/library/ee692796.aspx Fun Things You Can Do With the Get-ChildItem Cmdlet] (Microsoft TechNet)
[http://technet.microsoft.com/en-us/library/ee692796.aspx Fun Things You Can Do With the Get-ChildItem Cmdlet] (Microsoft TechNet)
Line 43: Line 42:
</syntaxhighlight>
</syntaxhighlight>


Just file names:
=== Limit listing to file names ===


<syntaxhighlight lang="powershell">
<syntaxhighlight lang="powershell">
Line 51: Line 50:
</syntaxhighlight>
</syntaxhighlight>


Recursive:
=== Recursive listing ===
 
<syntaxhighlight lang="powershell">
<syntaxhighlight lang="powershell">
> gci -recursive
> gci -recursive
Line 57: Line 57:
> gci -rec
> gci -rec
</syntaxhighlight>
</syntaxhighlight>
[[Category:Web Development]]

Revision as of 18:40, 30 November 2015

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\"

Recursive search

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

Recursive search filtered 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

Limit listing to file names

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

Recursive listing

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