Chirag Nagrekar has Published 465 Answers

How to retrieve the content of the file in PowerShell?

Chirag Nagrekar

Chirag Nagrekar

Updated on 16-Mar-2020 07:47:36

To retrieve the content of the file in PowerShell, you need to use Get-Content cmdlet. For example, we are going to retrieve the content of the text file called Aliases.txt from a specific location.ExampleGet-Content D:\Temp\aliases.txtOutputPS C:\WINDOWS\system32> Get-Content D:\Temp\aliases.txt # Alias File # Exported by : admin # Date/Time : 26 January 2020 19:20:24 # Computer : DESKTOP-9435KM9 "foreach", "ForEach-Object", "", "ReadOnly,  AllScope" "%", "ForEach-Object", "", "ReadOnly,  AllScope" "where", "Where-Object", ... Read More

What is Get-Content in PowerShell used for?

Chirag Nagrekar

Chirag Nagrekar

Updated on 16-Mar-2020 07:45:32

Get-Content cmdlet in PowerShell is useful for retrieving the contents of the file or the function. This cmdlet is introduced in PowerShell 3.0. When a file is read by this cmdlet, it reads one line at a time and finally returns the whole content as a collection of objects.SyntaxGet-Content   ... Read More

How to clear the content of the recycle bin in PowerShell?

Chirag Nagrekar

Chirag Nagrekar

Updated on 16-Mar-2020 07:42:35

Recycle bin in Windows operating system is to store the soft-deleted data. Soft deleted data means, data that is not deleted with (SHIFT + DEL) button but simply deleted with DEL button. Each local disk has its own configured recycle bin space.To delete the recycle bin data using GUI, you ... Read More

How to use PowerShell Break with the Label.

Chirag Nagrekar

Chirag Nagrekar

Updated on 12-Mar-2020 07:30:16

When the break statement is mentioned with the Label, PowerShell exits to the label instead of exiting the current loop.Example$i = 1 while ($i -lt 10) {    Write-Output "i = $i"    if($i -eq 5){       Write-Output "Break statement executed"       Break :mylable    }    $i++ ... Read More

How to use PowerShell Break statement with the Switch command?

Chirag Nagrekar

Chirag Nagrekar

Updated on 12-Mar-2020 07:28:38

In Switch command, when the single value passed and if it matches the condition then the loop gets automatically exited but when there are multiple values passed and if the value matches the first condition and if you want to terminate the loop then you can use the Break statement. ... Read More

How to use PowerShell break statement in foreach loop?

Chirag Nagrekar

Chirag Nagrekar

Updated on 12-Mar-2020 07:27:28

You can use the PowerShell Break statement with the foreach loop as mentioned below.Exampleforeach($obj in (Get-ChildItem D:\Temp)){    Write-Output $obj    if($obj.Name -eq "cars.xml"){Break} }OutputDirectory: D:\Temp Mode LastWriteTime  Length Name ---- -------------  ... Read More

How to use PowerShell break statement with the For loop?

Chirag Nagrekar

Chirag Nagrekar

Updated on 12-Mar-2020 07:25:17

To use the Break statement with the For loop, consider the below example.Examplefor($i=1; $i -le 10; $i++){    Write-Host "i = $i"    if($i -eq 5){break} }Outputi = 1 i = 2 i = 3 i = 4 i = 5Here, when the value of $i reaches to 5, For loop gets terminated.To use the Break with the nested For loop.Inner For loop Break ... Read More

How to use PowerShell Break statement with the While Loop?

Chirag Nagrekar

Chirag Nagrekar

Updated on 12-Mar-2020 07:21:53

You can use the break statement with both the While loop and the Do-While loop.To use the Break with a while loop, see the example below.Example$i = 1 While($i -ne 10){    Write-Output $i    if($i -eq 5){break}    $i++ }Output1 2 3 4 5In the above example, the loop terminates when the value ... Read More

What is PowerShell Break statement?

Chirag Nagrekar

Chirag Nagrekar

Updated on 12-Mar-2020 07:14:17

The break statement in PowerShell is useful for terminating the loop. When the break statement is executed inside the inner loop, it terminates that particular loop execution and when it is placed in the outer loop, it terminates the entire loop including child loop(s).Break statement is used with Foreach, For, ... Read More

How to copy files/folders to the remote location in the PowerShell?

Chirag Nagrekar

Chirag Nagrekar

Updated on 12-Mar-2020 07:12:25

To copy files or folders to or from the remote location, you need to provide the UNC path of the items. For example, here we are going to copy a file from the local computer to a remote computer called Test-PC.Copy-Item D:\Temp\PowerShellcommands.csv -Destination \\Test- PC\Sharedpath\PScommands.ps1 -PassThruHere, file PowerShellcommands.csv is copied ... Read More

Advertisements