Found 2039 Articles for Microsoft Technologies

Explain the PowerShell Advanced Function.

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

720 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

19K+ 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

288 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

948 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

How to sort the output in PowerShell?

Chirag Nagrekar
Updated on 07-Apr-2020 12:25:24

11K+ Views

To sort the output in the PowerShell you need to use Sort-Object Pipeline cmdlet. In the below example, we will retrieve the output from the Get-Process command and we will sort the, according to memory and CPU usage.ExampleGet-Process | Sort-Object WorkingSet | Select -First 10OutputHandles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName -------  ------    -----      -----     ------     --  -- -----------       0       0       60          8                 0 ... Read More

How to check the PowerShell version installed in local and remote systems?

Chirag Nagrekar
Updated on 07-Apr-2020 11:58:36

2K+ Views

To check the PowerShell version installed in your system, you can use either $PSVersionTable or $host command.Check if $host command available in remote servers.Open the PowerShell console in the system and run the command $PSVersionTable.$PSVersionTableOutputPS C:\WINDOWS\system32> $PSVersionTable Name                           Value ----                             ----- PSVersion                        5.1.18362.628 PSEdition                        Desktop PSCompatibleVersions       ... Read More

How to edit the CSV file using PowerShell?

Chirag Nagrekar
Updated on 26-Mar-2020 07:32:48

7K+ Views

To edit the CSV file using PowerShell, you need to use the below commands.We already have the CSV file output.csv, we will import this file first.$csvfile = Import-csv C:\temp\Outfile.csvOutputBelow is the output of the CSV file.EMP_Name    EMP_ID     CITY --------    ------     ---- Charles     2000       New York James       2500       Scotland Charles     3000       PolandWe need to update the above file. We will change the CITY of the EMP_ID ‘3000’ to MUMBAI. If we update the CITY name by the EMP_Name, it ... Read More

How to append data into a CSV file using PowerShell?

Chirag Nagrekar
Updated on 26-Mar-2020 07:28:05

43K+ Views

To append the data into CSV file you need to use –Append parameter while exporting to the CSV file.In the below example, we have created a CSV fileExample$outfile = "C:\temp\Outfile.csv" $newcsv = {} | Select "EMP_Name", "EMP_ID", "CITY" | Export-Csv $outfile $csvfile = Import-Csv $outfile $csvfile.Emp_Name = "Charles" $csvfile.EMP_ID = "2000" $csvfile.CITY = "New York" $csvfile | Export-CSV $outfile Import-Csv $outfileNow we need to append the below data to the existing file. So first we will import the csv file into a variable called $csvfile$csvfile = Import-Csv $outfile $csvfile.Emp_Name = "James" $csvfile.EMP_ID = "2500" $csvfile.CITY = "Scotland"Once data is inserted into ... Read More

How to create a CSV file manually in PowerShell?

Chirag Nagrekar
Updated on 26-Mar-2020 07:23:52

5K+ Views

There are few methods to create a CSV file in the PowerShell, but we will use the best easy method to create it.First, to create a CSV file in PowerShell, we need to create headers for it. But before it, we need output file name along with its path.$outfile = "C:\temp\Outfile.csv"Here we have given output file name Outfile.csv in the path C:\temp.Now we will create headers, $newcsv = {} | Select "EMP_Name", "EMP_ID", "CITY" | Export-Csv $outfileHere, we have created a CSV file and exported a file to the newly created file.We will now import this file to check if ... Read More

Advertisements