Found 2039 Articles for Microsoft Technologies

How to create a new local user in windows using PowerShell?

Chirag Nagrekar
Updated on 17-May-2021 12:20:05

3K+ Views

To create a new local user in the Windows operating system using PowerShell, we can use the New-LocalUser cmdlet. The below command will create the TestUser with no password.New-LocalUser -Name TestUser -NoPasswordOutputName Enabled Description ---- ------- ----------- TestUser TrueTestUser account has been enabled here. To provide the password for the user, the password should be in the secure string format. We can pass the password as shown below.$pass = "Admin@123" | ConvertTo-SecureString -AsPlainText -Force New-LocalUser -Name TestUser -Password $passThe above commands will create the TestUser with the password. To add the password and account-related settings we can directly provide parameters ... Read More

How to validate the path with the PowerShell function parameter?

Chirag Nagrekar
Updated on 17-May-2021 12:42:17

2K+ Views

To validate the file or folder path inside the PowerShell function parameter, we need to use the ValidateScript command. Generally, we write the script as below to validate the path.function Validate-Path{    param(       [parameter(Mandatory)]       [String]$Path    )    if(Test-Path $Path) {Write-Output "Path is valid"}    else{Write-Output "Path is invalid"} }OutputPS C:\> Validate-Path -Path C:\Temp Path is validWe can add similar functionality inside the function parameter with the validatescript argument so the script will throw the error initially at the parameters check. See below, function Validate-Path{    param(       [parameter(Mandatory)]       ... Read More

How to install MSI file using batch file in PowerShell?

Chirag Nagrekar
Updated on 17-May-2021 12:17:21

3K+ Views

Let say we have one MSI file and we need to install the MSI file on the remote computers using PowerShell but that MSI file should be deployed with the Batch file and should be executed using PowerShell.In this example, we have a 7-zip MSI file and batch file we will first write the installation instructions as shown below.msiexec /i "C:\temp\7z1900-x64.msi" /quietWe have the installation MSI package located at the C:\temp location. We will save the above instruction inside the 7ZipInstaller.bat file.Now we need to call the batch file as shown below. −Wait will wait for the batch file to ... Read More

How to retrieve the Azure VM RAM and CPU size using PowerShell?

Chirag Nagrekar
Updated on 17-May-2021 12:08:03

7K+ Views

Azure VM RAM and CPU size depend on the hardware profile chosen for the VM. In this example, we will retrieve VM (TestMachine2k16) hardware profile and then we can find how much RAM or CPU is allocated to it.To get the Size of the Azure VM, PS C:\> $azvm = Get-AzVM -VMName 'TestMachine2k16' PS C:\> $azvm.HardwareProfile.VmSizeOutputStandard_DS2_v2You can check the above size on the Microsoft Azure website to know how much RAM and CPU are associated with it and another way using the PowerShell by using the Get-AZVmSize command.PS C:\> $vmsize = $azvm.HardwareProfile.VmSize PS C:\> Get-AzVMSize -VMName $azvm.Name -ResourceGroupName $azvm.ResourceGroupName | ... Read More

How to retrieve the Azure VM Subscription Name using PowerShell?

Chirag Nagrekar
Updated on 17-May-2021 12:07:25

2K+ Views

Once you are connected to the Azure Account, there are possibilities that the Get-AzVM will display the VMs from all the Azure Subscriptions.To find the specific Azure VM Subscription name, we will first retrieve the details of the VM using the Get-AzVM and it has an ID property that contains the Subscription ID and from the subscription ID property, we can retrieve the name of the Azure Subscription Name.PS C:\> $azvm = Get-AzVM -Name TestMachine2k16 PS C:\> $subid = ($azvm.Id -split '/')[2] PS C:\> (Get-AzSubscription -SubscriptionId $subid).NameThe above will retrieve the name of the subscription.

How to retrieve the Azure VM resource group using PowerShell?

Chirag Nagrekar
Updated on 17-May-2021 12:06:52

2K+ Views

To retrieve the azure VM resource group using PowerShell, we first need to retrieve the Azure VM details using Get-AZVm and then we can use its property called ResourceGroup.Before getting the details of the Azure VM, make sure that you are connected to the Azure Account using the Connect-AzAccount command.In this example, we are going to retrieve the Azure VM named TestMachine2k16 to retrieve the VM details.$azvm = Get-AzVM -Name TestMachine2k16To get the resource group name, use its property ResourceGroupName.PS C:\> $azvm.ResourceGroupName ANSIBLETESTRG

How to eject the USB device from the system using PowerShell?

Chirag Nagrekar
Updated on 17-May-2021 12:05:55

5K+ Views

To eject the USB device from the system, we first need to get the USB device using PowerShell. WMI class Win32_Volume will help us to find the USB device.We know that all removal devices using the DriveType '2'. So we will filter out the USB device among the listed devices.PS C:\> $usbdev = gwmi win32_volume | where{$_.DriveType -eq '2'}The below commands will helpful to unallocated the USB from the system.PS C:\> $usbdev.DriveLetter = $null PS C:\> $usbdev.Put()OutputPath : \localhost\root\cimv2:Win32_Volume.DeviceID="\\?\Volume{6e4d6f1e-a8c2-11eb-9493-005056c00008}\" RelativePath : Win32_Volume.DeviceID="\\?\Volume{6e4d6f1e-a8c2-11eb-9493-005056c00008}\" Server : localhost NamespacePath : root\cimv2 ClassName : Win32_Volume IsClass : False IsInstance : True IsSingleton : FalseAnd ... Read More

How to check if the USB device is connected to the system using PowerShell?

Chirag Nagrekar
Updated on 17-May-2021 12:01:49

3K+ Views

To retrieve the USB-connected devices using Powershell, we need to retrieve all the drives using the WMI object or CIM Instance and need to filter the win32_diskdrive class with the USB as shown below.So basically, USB devices and other removable devices have the drivetype '2'. You can search with the InterfaceType or the DriveType.WMI commands, gwmi win32_diskdrive | where{$_.Interfacetype -eq "USB"}Alternatively, With the CIM commands, Get-CimInstance -ClassName Win32_DiskDrive | where{$_.InterfaceType -eq 'USB'}orGet-CimInstance -ClassName Win32_LogicalDisk | where{$_.DriveType -eq '2'}If there is no USB device connected to the system, there will be no output. To retrieve the USB disk on the remote ... Read More

How to get the Azure VM DNS name using PowerShell?

Chirag Nagrekar
Updated on 28-Apr-2021 13:46:01

2K+ Views

We can find the DNS name of the VM from the Azure Portal as shown below from the Azure VM Overview tab.This DNS setting is associated with the Public IP address. To retrieve the DNS settings we first need to retrieve the Public IP details. For this example, suppose we have the Azure VM TestMachine2k16 and we need to retrieve its DNS settings (Assuming you are connected to the proper Azure account and the subscription).$vm = Get-AzVM -VMName TestMachine2k16 $pubip = Get-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName | where{$_.Id -match $vm.Name}$pubip variable has the attached Public IP properties. You can retrieve the DNS ... Read More

How to retrieve the Azure VM vNet name using PowerShell?

Chirag Nagrekar
Updated on 28-Apr-2021 13:45:09

1K+ Views

To retrieve the Azure Virtual Network (vNet) or subnet name, we need the first name of the network interface of the VM. Below is the command to retrieve the name of the network interface.$vm = Get-AzVM -VMName Testmachine2k16TestMachine2k16 is the Azure VM name. Assuming this VM has a single NIC attached.PS C:\> $nic = $vm.NetworkProfile.NetworkInterfaces PS C:\> $networkinterface = ($nic.id -split '/')[-1] PS C:\> $networkinterface testmachine2k16619So our NIC name is stored inside the $NetworkInterface variable.If you have the multiple NICs attached, then use the below command to retrieve the NIC details.$nics = $vm.NetworkProfile.NetworkInterfaces foreach($nic in $nics) {    ($nic.Id -split ... Read More

Advertisements