Found 463 Articles for PowerShell

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

529 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.

How to change Pagefile settings using PowerShell?

Chirag Nagrekar
Updated on 05-Oct-2020 11:18:47

8K+ Views

To change the pagefile settings, we will divide this into multiple sections. First, when Pagefile is automatically managed, we can’t modify the settings so we need to remove that box. In GUI that box can be unchecked in Virtual memory settings.Code to uncheck the above box.$pagefile = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges $pagefile.AutomaticManagedPagefile = $false $pagefile.put() | Out-NullSo once the above code is executed, other fields will be enabled. We now need to Customize the size of the pagefile in the below image by providing Initial and Maximum size using PowerShell.Here we have already the pagefile on the C: while on the ... Read More

How to get pagefile settings using PowerShell?

Chirag Nagrekar
Updated on 05-Oct-2020 11:32:59

7K+ Views

Pagefile also Known as Virtual Memory file in the Windows Operating system is a very useful part of the OS. It helps to reduce the burden on the Physical memory by storing some paging file in the file call Pagefile.sys. Generally, this file in windows OS is stored in C:\ unless it is modified.You can check the Pagefile settings in the Windows GUI usingSystem Properties → Advanced → Performance → Settings → Advanced → Virtual Memory → Change.We have made a few blocks and circles in the above pagefile properties image. We will see them one by one.First, to check ... Read More

Advertisements