Chirag Nagrekar

Chirag Nagrekar

394 Articles Published

Articles by Chirag Nagrekar

394 articles

How to install the MSI file using PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 06-Nov-2023 25K+ Views

To install the MSI file with PowerShell, we can use cmdlet Start-Process.Let say we want to install the 7ZIP MSI file on the local computer and we have downloaded and stored the source file on the C:\temp location. Once we run the below command it will start the MSI installation.Start-Process C:\Temp\7z1900-x64.msiThe above command will open the GUI to install the MSI file but we use PowerShell to avoid GUI interaction so we can add the argument to install the file.If you check which arguments are available to run the MSI file, use the below command.C:\Temp\7z1900-x64.msi /?It will popup box like ...

Read More

How to add the user to the local Administrators group using PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 04-Nov-2023 45K+ Views

To add the AD user or the local user to the local Administrators group using PowerShell, we need to use the Add-LocalGroupMember command.To add the local user to the local Administrators group, Add-LocalGroupMember -Group Administrators -Member TestUser -VerboseThe above command will add TestUser to the local Administrators group. You can provide any local group name there and any local user name instead of TestUserYou can also add the Active Directory domain user to the Local Administrators group by providing the domain name if the computer is in the same domain.For example, we will add the Beta user from the AutomationLab ...

Read More

How to change Azure Subscription in PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 03-Nov-2023 36K+ Views

To change the azure subscription using PowerShell, we can use the Select-AZSubscription command. When you use this command, you can use either the subscription ID, Subscription Name, or the Tenant ID.ExampleWith Subscription Name,Select-AzSubscription -SubscriptionName 'Visual Studio'With TenantID,Select-AzSubscription -Tenant 'XXXX-XXXXX-XXXXXXX-XXXX'With Subscription ID,Select-AzSubscription -SubscriptionId 'XXXX-XXXXX-XXXXXXX-XXXX'Sometimes on console messages will appear that one or more subscriptions are active. In that case, you can switch the other subscription using the Set-AZContext command and you can use subscription ID or the Name for it.ExampleSet-AzContext -SubscriptionId "xxxx-xxxx-xxxx-xxxx" OrSet-AzContext -SubscriptionName "Visual Studio"

Read More

How to add/remove values in the array in PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 03-Nov-2023 45K+ Views

An array is always a fixed size. To add value to the array, you need to create a new copy of the array and add value to it. To do so, you simply need to use += operator.For example, you have an existing array as given below.$array = 1, 2, 3, 4, 5To add value “Hello” to the array, we will use += sign.$array += "Hello"Now, we will check the output of the array.We have another method to add value to the array. By Add() operation of the array.$array.Add("Hi")When you use the above method to add a variable in the ...

Read More

How to get the System uptime with PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 01-Nov-2023 50K+ 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 get the path of the currently executing script in PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 01-Nov-2023 46K+ Views

To get the full path of the script we need to use the $myInvocation command. This is an automatic variable and it is only invoked when the script or the function is executed.$MyInvocation.MyCommand.Path command is useful to get the full path of the script where it resides while $MyInvocation.MyCommand.Name is useful to get the name of the script. Example$mypath = $MyInvocation.MyCommand.Path Write-Output "Path of the script : $mypath"OutputPS C:\WINDOWS\system32> C:\Temp\TestPS.ps1 Path of the script : C:\Temp\TestPS.ps1Please note that we are running the above script from the System32 directory and the output path is C:\temp. To get the script directory, we can ...

Read More

How to get the local Administrators group members using PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 01-Nov-2023 52K+ Views

To get the local Administrators group members using PowerShell, you need to use the GetLocalGroupMember command. This command is available in PowerShell version 5.1 onwards and the module for it is Microsoft.PowerShell.LocalAccounts. This module is not available in the 32-bit PowerShell version but on a 64-bit system.In the below example, we need to retrieve the Local Administrators group members, ExampleGet-LocalGroupMember -Group "Administrators"OutputObjectClass    Name          PrincipalSource -----------    ----          --------------- User          LABDOMAIN\delta Group         LABDOMAIN\Domain Admins User          TEST1-WIN2K12\Administrator User       ...

Read More

How to export output to excel in PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 01-Nov-2023 61K+ Views

As of now, there is no built-in command like CSV (Export-CSV) to export output to the excel file but we can use the Out-File command to export data to excel or any other file format.Let’s use Out-File to export the output of the Get-Processes command to an excel file.Get-Process | Out-File C:\Temp\processes.xlsThe above command will get all the running processes on the local system to the excel file. Let’s check the output of the excel file. You can see that the output is not in the proper excel format.One other way is to install the ImportExcel module for excel. It ...

Read More

How to get environment variable value using PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 31-Oct-2023 88K+ Views

Environment variables are an essential part of the Operating System. They store various information like the path of the system files and folders, the number of processors system running, current user details, and more. Processes and programs utilize these environment variables to retrieve the data for their execution.Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command.Get-ChildItem -Path Env:Name                        Value ----                       ...

Read More

How to Set environment variables using PowerShell?

Chirag Nagrekar
Chirag Nagrekar
Updated on 29-Aug-2023 281K+ Views

To set the environmental variable using PowerShell you need to use the assignment operator (=). If the variable already exists then you can use the += operator to append the value, otherwise, a new environment variable will be created.For example, there is no AZURE_RESOURCE_GROUP environment variable that exists in the system. We can create it as below.$env:AZURE_RESOURCE_GROUP = 'MyTestResourceGroup'Now when you check the environment variables in the system, you will get the above variable name.PS C:\Windows\system32> dir env: Name                            Value ----             ...

Read More
Showing 1–10 of 394 articles
« Prev 1 2 3 4 5 40 Next »
Advertisements