Powershell console commands

 

To begin with, it is not always necessary to write a script for PowerShell. Especially for administrators the PowerShell console offers a possibility to execute certain tasks via commands.

Pipe

Commands that start with "get-" usually return one or more objects. As an example, the command "get-service" returns all services of the computer. "get-service" can also be restricted to one or more services with the additional parameter -name. The result of the query can be passed to another command with the pipe symbol "|", e.g. start-service:

 get-service -name defragsvc | start-service

get-service passes the result to the start-service command which then starts the service.

from-to

PS C:\Users> 1..3
1
2
3

Powershell pipe from to number multi-digit

In PowerShell it is possible to implement Foreach loops for simple commands via a pipeline (Pipe).

Simple loop from-to by means of pipe

With the following notation a simple loop from 1-xx can be formed:

PS C:\temp> 1..4 | %{ $_ }
1
2
3
4

"%" is an alias for "foreach", $_ is the variable for values passed via the pipe "|".

from-to with preceding zeros (0):

For creating multiple objects the format can be specified with -f. 

PS C:\temp> 9..11 | %{ write-output $('{0:0000}' -f $_)}
0009
0010
0011

Instead of "write-output", other commands can of course be executed within the Foreach loop: {}. As a concrete example, a certain number of computer objects could be created in Active Directory:

PS C:\temp> 1..100 | % { New-ADComputer -Name "TESTPC$('{0:0000}' -f $_)" -SamAccountName "TESTPC$('{0:0000}' -f $_)" -path "OU=Computers,DC=domain,DC=local" }

The command would create the computer accounts TESTPC0001 to TESTPC0100.

PS C:\temp> 9..11 | %{ write-output $('{0:0000}' -f $_)}
0009
0010
0011
positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details