Difference between revisions of "Useful PowerShell Commands"
Jump to navigation
Jump to search
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Get-ComputerInfo | Get-ComputerInfo | ||
==Accessing the properties of a command == | |||
Get-Computerinfo -Properties csusername | Get-Computerinfo -Properties csusername | ||
Get-Computerinfo -Properties csusername | Get-Member | Get-Computerinfo -Properties csusername | Get-Member | ||
Get-ComputerInfo | Format-List OsType,osName,csusername | Get-ComputerInfo | Format-List OsType,osName,csusername | ||
== Write output to the screen, like Echo == | |||
Write-Output "Hello World" | Write-Output "Hello World" | ||
== Read Text file == | |||
Get-Content filename.txt | Get-Content filename.txt | ||
Line 16: | Line 16: | ||
Get-TimeZone | Get-TimeZone | ||
<h3>[[Update Windows From PowerShell]]</h3> | <h3>[[Update Windows From PowerShell]]</h3> | ||
==Import csv file The pipe it to another function== | |||
import-csv .\filename.csv | | import-csv .\filename.csv | | ||
== Pipe files from explorer into text file == | |||
get-childitem . -name >> filenames.txt | |||
get-childitem . -file | foreach-object{$_.BaseName} > filenames.txt | |||
==Rename Files using csv list / excel file (exported to CSV)== | |||
<p>If you execute the script in the same directory as the files, you don't have to add the full path, but if it's in a different directory the the full path is needed</p> | <p>If you execute the script in the same directory as the files, you don't have to add the full path, but if it's in a different directory the the full path is needed</p> | ||
<p>You can start with an excel file. Header has to be <span class="high"> Path</span> and <span class="high">NewName </span></p> | <p>You can start with an excel file. Header has to be <span class="high"> Path</span> and <span class="high">NewName </span> Then export the file as <span class="high">CSV</span></p> | ||
{| class="wikitable" style="margin:auto" | {| class="wikitable" style="margin:auto" | ||
|+ | |+ Sample Excel File | ||
|- | |- | ||
! Path !! NewName | ! Path !! NewName | ||
Line 34: | Line 40: | ||
<p class="subhead"><span class="yb">Path </span> refers to the Old Filename and <span class="yb"> NewName </span>is the New Filename </p> | <p class="subhead"><span class="yb">Path </span> refers to the Old Filename and <span class="yb"> NewName </span>is the New Filename </p> | ||
<p>The reason why we add the <span class="high">Path,</span> and <span class="high">NewName </span> is because the <span class="high">Rename-Item</span> function accepts a Path string and a NewName string</p> | <p>The reason why we add the <span class="high">Path,</span> and <span class="high">NewName </span> is because the <span class="high">Rename-Item</span> function accepts a '''Path''' string and a '''NewName''' string</p> | ||
import-csv ./rename.csv | rename-item | import-csv ./rename.csv | rename-item | ||
< | <h2>The CSV File</h2> | ||
<pre> | <pre> | ||
Path,NewName | Path,NewName | ||
Line 46: | Line 52: | ||
</pre> | </pre> | ||
== Pass txt file to a command == | |||
<p class="subhead">The parentheses is needed to execute that portion first, then pass the content to the Test-Connection </p> | |||
Test-Connection -ComputerName (Get-Content .\filenamewithcomputernames.txt) | |||
== Get a Count of Item. Like WC in Linux == | |||
Get-ChildItem . -name | Measure-Object | |||
<pre> | |||
Count : 417 | |||
Average : | |||
Sum : | |||
Maximum : | |||
Minimum : | |||
Property : | |||
</pre> | |||
==Sorting Object in the pipeline == | |||
Get-Process | Sort-Object ID,ProcessName -Unique | |||
'''Sorts by ID First Then ProcessName''' | |||
<pre> | |||
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName | |||
------- ------ ----- ----- ------ -- -- ----------- | |||
0 0 60 8 0 0 Idle | |||
10836 0 60 3356 4 0 System | |||
0 0 176 102024 332 0 Secure System | |||
0 25 6876 50164 376 0 Registry | |||
1653 20 10408 20084 1068 0 svchost | |||
208 8 1580 6360 1092 0 WUDFHost | |||
899 30 2572 6316 1152 0 csrss | |||
</pre> | |||
==[[Main_Page| Home]] - [[PowerShell|Category]]== | ==[[Main_Page| Home]] - [[PowerShell|Category]]== |
Latest revision as of 19:11, 22 October 2024
Get-ComputerInfo
Accessing the properties of a command
Get-Computerinfo -Properties csusername Get-Computerinfo -Properties csusername | Get-Member Get-ComputerInfo | Format-List OsType,osName,csusername
Write output to the screen, like Echo
Write-Output "Hello World"
Read Text file
Get-Content filename.txt
Rename-Computer Restart-Computer Get-Date Get-TimeZone
Update Windows From PowerShell
Import csv file The pipe it to another function
import-csv .\filename.csv |
Pipe files from explorer into text file
get-childitem . -name >> filenames.txt
get-childitem . -file | foreach-object{$_.BaseName} > filenames.txt
Rename Files using csv list / excel file (exported to CSV)
If you execute the script in the same directory as the files, you don't have to add the full path, but if it's in a different directory the the full path is needed
You can start with an excel file. Header has to be Path and NewName Then export the file as CSV
Path | NewName |
---|---|
cat.jpg | cat01.jpg |
dog.jpg | dog02.jpg |
rat.jpg | rat03.jpg |
Path refers to the Old Filename and NewName is the New Filename
The reason why we add the Path, and NewName is because the Rename-Item function accepts a Path string and a NewName string
import-csv ./rename.csv | rename-item
The CSV File
Path,NewName dog.jpg,222-dog.jpg rat.jpg,333-rat.jpg cat.jpg,111-cat.jpg
Pass txt file to a command
The parentheses is needed to execute that portion first, then pass the content to the Test-Connection
Test-Connection -ComputerName (Get-Content .\filenamewithcomputernames.txt)
Get a Count of Item. Like WC in Linux
Get-ChildItem . -name | Measure-Object
Count : 417 Average : Sum : Maximum : Minimum : Property :
Sorting Object in the pipeline
Get-Process | Sort-Object ID,ProcessName -Unique
Sorts by ID First Then ProcessName
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 0 0 60 8 0 0 Idle 10836 0 60 3356 4 0 System 0 0 176 102024 332 0 Secure System 0 25 6876 50164 376 0 Registry 1653 20 10408 20084 1068 0 svchost 208 8 1580 6360 1092 0 WUDFHost 899 30 2572 6316 1152 0 csrss