Microsoft Technologies Articles - Page 189 of 204

How to remove empty string/lines from PowerShell?

Chirag Nagrekar
Updated on 15-May-2020 12:57:43

10K+ Views

In many instances, you need to remove empty lines or strings from the PowerShell string array or the files. In this article instead of removing empty string, we will get the result or filter the output from lines that are not empty. In this way, we can get the output without empty lines.Consider the example below, we have a file called EmptryString.txt and we need to remove empty lines from the content.The content of the text file is as below.PS C:\Windows\System32> Get-Content D:\Temp\EmptyString.txt This is example of empty string PowerShell PowerShell DSC String Array HelloYou just need to apply the ... Read More

How to get the port number of the processes using PowerShell?

Chirag Nagrekar
Updated on 04-May-2020 13:22:15

3K+ Views

When we use Get-Process cmdlet in PowerShell, it doesn’t have properties to get Port number the processes use. So here we will write a function that will provide us the ports number associated with the processes.There is one windows command NETSTAT which provides the Port number and the associated process ID but doesn’t provide the process name. We have Get-Process command which provides the process name and the PID (Process ID) so we can write a program that can associate both the commands and we can retrieve the process ID, local address, remote address, and if the state of the ... Read More

Explain ValueFromPipeline in PowerShell Advanced Functions.

Chirag Nagrekar
Updated on 04-May-2020 07:15:45

5K+ Views

Consider the below example, we have created Advanced function to get the specific process information like Process Name, Process ID (PID), Start Time, Responding status, etc.function Get-ProcessInformation{    [cmdletbinding()]    param(       [Parameter(Mandatory=$True)]       [string[]]$name    )    Begin{       Write-Verbose "Program started"    }    Process{       Write-Verbose "Extracting Process Inforamtion" Get-Process $name | Select Name, ID, StartTime, Responding | ft - AutoSize    }    End{       Write-Verbose "Function ends here"    } }Above is the advanced function example. When we run the above command, it will give ... Read More

Explain AllowEmptyString() and AllowEmptyCollection() in PowerShell Advanced Function.

Chirag Nagrekar
Updated on 04-May-2020 07:04:15

4K+ Views

AllowEmptyString() attribute works with the string variable and AllowEmptyCollection() work with the array of different data types (Collection).Consider the example below. Here, we are using a mandatory variable $name which is a string and $stringarray which is a string array.function print_String{    [cmdletbinding()]    param(       [parameter(Mandatory=$True)]       [string]$name,    )    Write-Output "Writing a single string"    $name }If we get the output of the above variable it will generate an error below.PS C:\WINDOWS\system32> print_String cmdlet print_String at command pipeline position 1 Supply values for the following parameters: name: print_String : Cannot bind argument to ... Read More

Explain the Mandatory Attribute in PowerShell Advanced Function.

Chirag Nagrekar
Updated on 04-May-2020 07:01:04

471 Views

We have an example of PowerShell advanced function below and we will try to understand how mandatory parameter works.function math_Operation{    [cmdletbinding()]    param([int]$val1, [int]$val2)    Write-Host "Multiply : $($val1*$val2)"    Write-Host "Addition : $($val1+$val2)"    Write-Host "Subtraction : $($val1-$val2)"    Write-Host "Divide : $($val1+$val2)" }When you execute the above example and don’t supply values then the script won’t ask you for the values, by default it will take the values and execute the program. See the execution below.PS C:\WINDOWS\system32> math_Operation Multiply : 0 Addition : 0 Subtraction : 0 Divide : 0Even if you have mentioned two variables ($val1, ... Read More

Explain the PowerShell Advanced Function.

Chirag Nagrekar
Updated on 04-May-2020 06:56:58

767 Views

Before starting the Advance PowerShell function, assuming we know about the PowerShell function. You can check the explanation on the PowerShell function below.https://www.tutorialspoint.com/explain-the-powershell-functionHere, we will take the math function example that calculates the different types of operations. We already have a code with the simple function as shown below.function math_Operation{    param([int]$val1, [int]$val2)    Write-Host "Multiply : $($val1*$val2)"    Write-Host "Addition : $($val1+$val2)"    Write-Host "Subtraction : $($val1-$val2)"    Write-Host "Divide : $($val1+$val2)" }The above example is of the simple function. When you run the above code and run the function from the terminal and you can notice you will ... Read More

How to pass the parameters in the PowerShell function?

Chirag Nagrekar
Updated on 17-Apr-2020 12:36:16

20K+ Views

You can pass the parameters in the PowerShell function and to catch those parameters, you need to use the arguments. Generally, when you use variables outside the function, you really don’t need to pass the argument because the variable is itself a Public and can be accessible inside the function. But in some cases we need to pass the parameters to the function and below is the example explains how you can write the code for it.The single parameter passed in function, function writeName($str){    Write-Output "Hi! there .. $str" } $name = Read-Host "Enter Name" writeName($name)Here, we are passing ... Read More

Explain variable / Array scope in PowerShell function.

Chirag Nagrekar
Updated on 17-Apr-2020 12:34:31

314 Views

Generally, when the variable is declared as a Public variable or outside the function in the script (not in any other variable or condition), you don’t need to pass that value to function because the function retains the value of the variable when the variable is initialized before the function is called.Examplefunction PrintOut{    Write-Output "your Name is : $name" } $name = Read-Host "Enter your Name" PrintOutIn the above example, $name variable is declared outside the function called PrintOut. So as the variable can be read inside the function, you can directly use the variable by its name.OutputEnter your ... Read More

Explain the PowerShell Function.

Chirag Nagrekar
Updated on 17-Apr-2020 12:32:23

983 Views

Function in a PowerShell is to reduce the redundancy of the repeated codes this means codes which are repeating, bind them into a function and call the function whenever necessary, so you don’t need to write codes multiple times.ExampleLet assume, you want to perform arithmetic operations (multiply, sum, divide and subtraction) of two values 5 and 4 in one go, so you will write different operations for two values or you will assign values to the variable called $val1 and $val2 and perform various operations on them as shown below in the example.$val1 = 5 $val2 = 4 $val1 ... Read More

How to use Measure-Object in PowerShell?

Chirag Nagrekar
Updated on 07-Apr-2020 12:21:33

2K+ Views

Measure-Object in PowerShell is used to measure the property of the command. There are various measurement parameters are available. For example, Average, Count, sum, maximum, minimum and more.ExampleGet-Process | Measure-ObjectOutputPS C:\WINDOWS\system32> Get-Process | Measure-Object Count       : 278 Average     : Sum         : Maximum     : Minimum     : Property    :Here, in the above output, there are total 278 processes are running. If you want to check the maximum memory usage then you can use WorkingSet property with − Maximum Parameter.Get-Process | Measure-Object -Property WorkingSet -MaximumOutputPS C:\WINDOWS\system32> Get-Process | Measure-Object ... Read More

Advertisements