Found 989 Articles for Software & Coding

How does PowerShell Pipeline work – Part 2?

Chirag Nagrekar
Updated on 16-Oct-2020 09:49:39

102 Views

In part-1 we have seen the PowerShell pipeline functionality using the ValueFromPipeline property. There is another cmdlet property known as ValueFromPipelineByPropertyName, which is also useful to know the PowerShell pipeline functionality.Like part-1 command, we can get this property name using the same Get-Command but the filter parameter we will use for the property is ValueFromPipelineByPropertyName.The below example is for the Stop-Service cmdlet.(Get-Command Stop-Service).ParameterSets.parameters | where{$_.ValueFromPipelineByPropertyName -eq 'True'} | Select Name, ParameterTypeOutputName ParameterType ---- ------------- Name System.String[]This means you can use Name property to stop the service. So here we will use Get-Service and its Name property to retrieve services and ... Read More

How does PowerShell Pipeline work – Part 1?

Chirag Nagrekar
Updated on 16-Oct-2020 09:44:27

118 Views

PowerShell is made easier with the Pipeline structure. With the Pipeline structure, we can pass the input of the left side command output or string to the right side of the command as the input. For example, Get-Service | Out-File c:\services.txtIn the above example, we are passing Get-Service output as an object to the Out-File command which is the right side of the Pipeline as the Input. Before going into detail about how the Pipeline works, we need to understand that every command we write produces output and that output is already formatted by the PowerShell using Pipeline.For example, Get-Process ... Read More

How to get the System uptime with PowerShell?

Chirag Nagrekar
Updated on 01-Nov-2023 20:16:48

37K+ Views

To get the Windows System uptime with PowerShell, we can use the CIM Instance method with class name Win32_OperatingSystem. Once you use the mentioned class there is a property called LastBootupTime which shows the date when the computer is last rebooted.ExampleGet-CimInstance -ClassName Win32_OperatingSystem | Select LastBootUpTimeOutputLastBootUpTime -------------- 9/29/2020 8:12:08 AMIf we check the datatype of the above output, it should be DateTime because of the output format.(Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime.Gettype()OutputIsPublic IsSerial Name     BaseType -------- -------- ----     -------- True     True     DateTime System.ValueTypeWe need now the uptime of the system in Days-Hours-Minutes format. So we will compare ... Read More

How to block ports on the Windows Operating System using PowerShell?

Chirag Nagrekar
Updated on 16-Oct-2020 09:39:26

3K+ Views

To block the port using PowerShell on the Windows OS, we need to change the firewall settings using the New-NetFirewallRule command.ExampleWe need to block the port 5985 on the computer. The below code will block all TCP Incoming requests on the 5985 port on the local computer.New-NetFirewallRule -DisplayName "Block WINRM HTTP Port" `                     -Direction Inbound `                     -LocalPort 5985 `                     -Protocol TCP `                     -Action Block To ... Read More

How to open a port in the Windows Operating System using PowerShell?

Chirag Nagrekar
Updated on 16-Oct-2020 09:35:20

2K+ Views

To open a port in the Windows Operating system, we need to know a few things. LikeFor which Profile we need to open port (Public, private, or Domain)? - OptionalWhich port do we need to open (port Number)?The direction of the port – Inbound (i.e Incoming requests) or Outbound (i.e. Outgoing requests).Protocol by name (TCP, UDP, ICMPv4, or ICMPv6) or Number (0-255).Once we have all the details we can open the port. In the below example, we need to open a port 5985 (WINRM HTTP) port on the computer which is currently blocked. So we will use the below command.New-NetFirewallRule -DisplayName "Allow WINRM HTTP Port" ` ... Read More

How to change files and folders attributes using PowerShell?

Chirag Nagrekar
Updated on 16-Oct-2020 09:28:49

8K+ Views

There are multiple files and folders attribute supported by the Windows Operating System. To check which attributes that files and folders support use DOS command attrib /?You can see the attributes listed like Read-Only, Archive, etc. You can set the attribute using PowerShell.For example, we have a file called TestFile.txt and its attribute is ReadOnly and we need to change it to the Archive.PS C:\> (Get-ChildItem C:\Temp\TestFile.txt).Attributes ReadOnlyChange Attribute code −$file = Get-ChildItem C:\Temp\TestFile.txt $file.Attributes = 'Archive'So we have set the attribute to the ‘Archive’ from ‘ReadOnly’ and when you check it, the attribute should be changed.PS C:\> (Get-ChildItem C:\Temp\TestFile.txt).Attributes ... Read More

How to retrieve files and folders attributes using PowerShell?

Chirag Nagrekar
Updated on 16-Oct-2020 09:20:11

2K+ Views

To retrieve files and folders attributes using PowerShell, you can use Get-Item or Get-ChildItem command. For example, We have a file called testfile.txt to get its attributes, PS C:\> Get-ChildItem C:\Temp\TestFile.txt |Select Name, Attributes Name Attributes ---- ---------- TestFile.txt ArchiveSo this file has the Archive attribute. To retrieve multiple files and folders' attributes, just refer to the folder name instead of the file name.Get-ChildItem C:\Temp -Recurse -Force | Select Name, FullName, Attributes Name ... Read More

How to get hidden files and folders using PowerShell?

Chirag Nagrekar
Updated on 05-Oct-2020 11:29:24

11K+ Views

To get hidden files and folders using PowerShell, we need to use the Get-ChildItem command with the - Hidden or -Force parameter.The difference between the two mentioned parameters is Hidden parameter only retrieves the hidden files and folders while the Force parameter retrieves all the files and folders including Hidden, read-only and normal files and folder.For example, We have one folder named Data inside folder C:\temp and we need to retrieve it.PS C:\> Get-ChildItem C:\Temp\ -Hidden Directory: C:\Temp Mode        LastWriteTime      Length Name ----        -------------     ------ ---- d--h-       ... Read More

How to remove pagefile on the specific drive using PowerShell?

Chirag Nagrekar
Updated on 05-Oct-2020 11:22:57

2K+ Views

In this article, we have a pagefile set on E: (System managed) and we need to remove the pagefile from E: So in the below image once we remove it, the pagefile should be “No Paging File”.To do so using PowerShell, we need to filter the pagefile on a specific drive and need to run the below code.$pagefileset = Gwmi win32_pagefilesetting | where{$_.caption -like 'E:*'} $pagefileset.Delete()You may want to reboot the server after removing pagefile.To change the above settings on the remote computer, use -ComputerName parameter in the GetWMIObject class.

How to change pagefile settings from custom to system managed using PowerShell?

Chirag Nagrekar
Updated on 05-Oct-2020 11:20:44

531 Views

To change the pagefile settings to system managed, we need to set InitialSize and MaximumSize parameters to 0. In the below example, we have E: has custom pagefile, not system managed and we need to convert it to the system managed.$pagefileset = Gwmi win32_pagefilesetting | where{$_.caption -like 'E:*'} $pagefileset.InitialSize = 0 $pagefileset.MaximumSize = 0 $pagefileset.Put() | Out-NullNow when you check the pagefile setting on E: it should be System managed.To change the settings on the remote computer use -ComputerName parameter in the Get-WmiObject method.

Advertisements