Editing
Windows PowerShell Cookbook
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[[Category:Powershell]] [[Category:Windows]] == 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"> > (new-object Net.WebClient).DownloadString("http://damienjay.com/") </syntaxhighlight> == 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 />
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information