PowerShell modules are stored in their module path. Module paths can be retrieved using an environmental variable $env:PSModulePath. To get a better view, we will split the variable path with a semicolon.$env:PSModulePath -split ';' C:\Users\Administrator\Documents\WindowsPowerShell\Modules C:\Program Files\WindowsPowerShell\Modules C:\Windows\system32\WindowsPowerShell\v1.0\ModulesYou can see the 3 paths above. Each path has Modules stored as per their scopes.Documents Path: Modules are stored in this path when you provide the scope – CurrentUser while installing the module.Program files path: Modules are stored in this path when AllUsers Scope is provided.System32 path: It is the default path for the module. Whenever Microsoft updates any PowerShell version or module, it ... Read More
To know which parameters are supported by cmdlets, Get-Command retrieves the properties of the command. For example, We need to find the Get-Process parameters so the Get-Command will retrieve the command information and Full List will provide you the properties.Get-Command Get-Process | flOnce you run the command, it shows the ParameterSets property and they are the parameters supported by cmdlets.PS C:\> (Get-Command Get-Process).ParameterSets |ft -AutoSize Name IsDefault Parameters ---- --------- ---------- Name True {Name, ComputerName, Module, FileVersionInfo..} ... Read More
Split-Path is to retrieve the part of the specified path, such as a parent folder, a subfolder, or a file name. It can also tell if the path is relative or absolute.This command supports a few parameters which help to retrieve the part of the specified path. Consider we have below executable file path and we will see how the Split-Path command will retrieve the parent folder and subfolder as well as the root directory.'C:\Temp\PsExec.exe'Default Split-Path command will retrieve the parent folder name of the file.PS C:\> Split-Path 'C:\Temp\PsExec.exe' C:\TempHere the default parameter is -Parent, which retrieves the parent folder path. Above command ... Read More
Ipconfig /all command also retrieves the DNS settings for all the network interfaces. This command can be run on both cmd and PowerShell. For example, ExamplePS C:\Users\Administrator> ipconfig /all Windows IP Configuration Host Name . . . . . . . . . . . . : Test1-Win2k16 Primary Dns Suffix . . . . . . . : labdomain.local Node Type . . . . . . . . . . . . : Hybrid IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . ... Read More
To get the IP address of the system we can use IPConfig command in cmd and the same command can be used in PowerShell. IPConfig command shows all the connected and disconnected adapters including IPv4 and IPv6. For example, ExamplePS C:\Users\Administrator> Ipconfig Windows IP Configuration Ethernet adapter Ethernet0: Connection-specific DNS Suffix . : IPv4 Address. . . . . . . . . . . : 192.168.0.104 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.0.1 Tunnel adapter isatap.{27E40122-664A-404D-A4C9-4E48C0363BC5}: ... Read More
Have we ever wondered, Write-Output and Write-Host both are used to print the strings or the output of the command then what is the difference between them?ExamplePS C:\> Write-Output "Test String" Test String PS C:\> Write-Host "Test String" Test StringThe output remains the same.The first major difference is storing the output using the Pipeline structure. Write-Output and Write-Host both support the pipeline structure for example, Example"Test String" | Write-Output Test String "Test String" | Write-Host Test StringThe output remains the same.The first major difference is storing the output using the Pipeline structure. Write-Output and Write-Host both support the pipeline ... Read More
Foreach-Object -Parallel command was introduced in PowerShell version 7, preview 3, and it is used for the parallel execution of the pipeline input and more details on this have been explained in this article.Please Note: Foreach-Object and Foreach commands are the same but we can’t write Foreach -Parallel command because there is no such command, we need to use the full command, Foreach-Object -Parallel.A simple example of running a parallel object.Example1..10 | ForEach-Object -Parallel{ $_ * 1000 Sleep 1 }Output1000 2000 3000 4000 5000 6000 7000 8000 9000 10000Let's compare the timings with and without -Parallel parameter.Parallel Execution$time1 = Measure-Command { ... Read More
To use the stopwatch in PowerShell, we need to use [System.Diagnostics.Stopwatch] class.We will create a new object for this class, $stopwatch = [System.Diagnostics.Stopwatch]::new()Below are the members of the above stopwatch mentioned class.PS C:\> $Stopwatch | gm TypeName: System.Diagnostics.Stopwatch Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() Reset Method void Reset() Restart Method void Restart() Start Method void Start() Stop Method void Stop() ToString Method string ToString() Elapsed Property timespan Elapsed {get;} ElapsedMilliseconds Property long ElapsedMilliseconds {get;} ElapsedTicks Property long ElapsedTicks {get;} IsRunning Property bool IsRunning {get;}You can see ... Read More
The ternary operator in PowerShell was introduced with the PowerShell version7.0. The ternary operator has ‘?’ (question mark) symbol and its syntax is, [Condition] ? (output if True) : (output if false)The left side of the ternary operator is condition and the right side is output based on the condition statement. The output from the condition is in the forms of the Boolean, if the condition is true, the True block will be executed and if the condition is false, the False block will be executed. For example, Example$a = 5; $b = 6 ($a -gt $b) ? "True" : ... Read More
Compare-Object command in PowerShell is used to compare two objects. Objects can be a variable content, two files, strings, etc. This cmdlet uses few syntaxes to show the difference between objects which is called side indicators.=> - Difference in destination object. Compare-Object "World" "Alpha" InputObject SideIndicator ----------- ------------- Alpha => World Compare-Object "World" "woRld" PS C:\> Compare-Object "World" "woRld" -IncludeEqual InputObject SideIndicator ----------- ------------- World ==Please note, Comparison-Object is not case sensitive. For the case-sensitive comparison, use -CaseSensitive parameter.PS C:\> Compare-Object "World" "woRld" -CaseSensitive InputObject SideIndicator ----------- ------------- woRld => World File2.txt ... Read More