Found 989 Articles for Software & Coding

What is the breakpoint in PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:54:14

214 Views

Breakpoint in the PowerShell is the part of the debugger in the PowerShell commands. We use the breakpoints in PowerShell mainly for troubleshooting and logging purpose.There are three ways to set the breakpoint in PowerShell.Line BreakPoint (Can set the breakpoint for single or multiple lines)Command BreakPoint (Can set the breakpoint for commands or functions)Variable Breakpoint (Can set the breakpoint on a variable or multiple variables).We can’t set the breakpoint for the remote computer. To set the breakpoint for the remote computer, we first need to copy the script to the remote computer and then need to set the breakpoint as ... Read More

How to check if the file is empty using PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:52:57

6K+ Views

To check if the file is empty using PowerShell, we can use the string method called IsNullorWhiteSpace(). This method provides result true if the file is empty or only contains the white spaces otherwise false.For example, We have a test2.txt text file which has whitespaces.Example[String]::IsNullOrWhiteSpace((Get-content C:\Test2.txt))OutputTrueBut if you have a file like CSV which contains few headers but the data is empty, in that case, Get-Content will show the wrong output because it will consider headers. For example, Example[String]::IsNullOrWhiteSpace((Get-content C:\Temp\NewUsers.csv))OutputFalse Because the file has headers.PS C:\> Get-Content C:\Temp\NewUsers.csv Name, FirstName, Surname, EMPNumber, CountryIn that case, we can use the Import-CSV ... Read More

How to open any file with its default application with PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:51:40

2K+ Views

To open any file using its default application, we can use the Invoke-Expression command. For example, we want to open a file on the C:\temp and the file name is NewUsers.CSV then you can run the below command.Invoke-Expression C:\Temp\NewUsers.csvThe above command will open the file from that location. If the default application is not set then Windows will ask for the default application to select.If you know any application name and which can be opened with the shortcut then you can directly type the name of the application. For example, Notepad.exe, Calc.exeGenerally, they can be opened directly but this command ... Read More

How to convert raw text to the CSV (Comma Separated Values) in PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:50:44

864 Views

We have a raw text example and to convert it into the CSV values, we can use below code.ExamplePS C:\> $text = "This is a PowerShell latest version" PS C:\> $text.Replace(' ', ', ')OutputThis, is, a, PowerShell, latest, versionIf there are multiple spaces between the keywords then the above replace command would go wrong. For example, ExamplePS C:\> $text = "This is a PowerShell latest version" PS C:\> $text.Replace(' ', ', ')OutputThis, , is, , , a, , , , PowerShell, latest, version So we can use another method as shown below.$text -replace '\s+', ' 'In the above command, \S ... Read More

How to get the list of opened applications in windows with PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:49:49

3K+ Views

We can get the list of opened applications in Windows using PowerShell using Get-Process command. Get-Process command shows the currently running processes in the foreground and the background as well.If we simply run the Get-Process command then it will give the process names and their associated process IDs and CPU, memory usage.PS C:\> Get-ProcessIf we check the members of the Get-Process, there is a MainWindowsTitle Property. This property indicates the Title of the opened application.In the below example, we will find the Application Name, associated process, and the ID of the application. For example, Get-Process | Select MainWindowTitle, ProcessName, Id ... Read More

How to delete the Organizational Unit (OU) from the active directory using PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:48:05

867 Views

To delete the OU from the Active Directory using PowerShell, we need to use the command Remove-ADOrganizationUnitRemove-ADOrganizationalUnit -Identity "OU=LabUsers, DC=Labdomain, DC=Local"If the OU is protected from the accidental delete, you will receive an Access is Denied error as shown below.Remove-ADOrganizationalUnit : Access is denied At line:1 char:1 + Remove-ADOrganizationalUnit -Identity "OU=LabUsers, DC=Labdomain, DC=Lo ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (OU=LabUsers, DC=Labdomain, DC=Local:ADOrganizationalUnit) [Remove-ADOrgan izationalUnit], UnauthorizedAccessException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.UnauthorizedAccessException, Microsoft.ActiveDirectory.Managem ent.Commands.RemoveADOrganizationalUnitSo for that, first you need to disable the protected mode using the Set-ADOrganizationalUnit command and then need to run the remove command as shown below.$ou = "OU=LabUsers, DC=Labdomain, ... Read More

How to delete all users from specific OU using PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:46:25

2K+ Views

To remove all users from the specific OU, we need to first retrieve the users from that OU. For example, we have 3 users in the OU called LABDOMAIN and we need to delete them all.Get-ADUser -SearchBase "OU=LabUsers, DC=labdomain, DC=local" -Filter *The above command will retrieve users from the specific OU and then we can use the Remove-ADUser command to delete them all.ExampleGet-ADUser -SearchBase "OU=LabUsers, DC=labdomain, DC=local" -Filter * | Remove-ADUser -Confirm:$false -Verbose-Confirm switch has been added to bypass the confirmation for all the users. The default confirmation is true.OutputVERBOSE: Performing the operation "Remove" on target "CN=JensonA, OU=LabUsers, DC=labdomain, DC=local". ... Read More

How to rename the drive letter in Windows using PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:45:13

280 Views

We can rename a drive letter of a disk using PowerShell with the Set-Partition command. An example is shown below.In the Windows OS, we have E: and we need to rename its partition to the F: You can run the below command.Set-Partition -DriveLetter 'E' -NewDriveLetter 'F'You can also run the Get-Partition command and Pipeline the Set-Partition command. For example, Get-Partition -DriveLetter 'E' | Set-Partition -NewDriveLetter 'F'If you want to rename the drive on the remote computer, then you need to take the CIMSession of the system and run the command. For example, $sess = New-CimSession -ComputerName 'Test1-Win2k12' Set-Partition -CimSession $sess ... Read More

How to create bulk users in the active directory using PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:44:25

1K+ Views

Using Random Account NameTo create bulk users in the AD using PowerShell, there are multiple methods. For example, let say if you want to create 50 sample users for the lab environment without considering the other required properties, then you can use the below command, $pass = Read-Host "Enter Account Password " -AsSecureString 1..50 | foreach{ New-ADUser -Name "TempUser$_" -AccountPassword $pass -Path "OU=LabUsers, DC=labdomain, DC=local" -ChangePasswordAtLogon $true -Enabled $true -PasThru}The above command will create 50 Temp users in the OU named LABUSERS and one time we need to enter the password while running the script and when users log in ... Read More

Where the PowerShell Modules are stored?

Chirag Nagrekar
Updated on 11-Nov-2020 13:07:57

25K+ Views

PowerShell modules are stored in their module path. Module paths can be retrieved using an environmental variable $env:PSModulePath. To get a better view, we will split the variable path with a semicolon.$env:PSModulePath -split ';' C:\Users\Administrator\Documents\WindowsPowerShell\Modules C:\Program Files\WindowsPowerShell\Modules C:\Windows\system32\WindowsPowerShell\v1.0\ModulesYou can see the 3 paths above. Each path has Modules stored as per their scopes.Documents Path: Modules are stored in this path when you provide the scope – CurrentUser while installing the module.Program files path: Modules are stored in this path when AllUsers Scope is provided.System32 path: It is the default path for the module. Whenever Microsoft updates any PowerShell version or module, it ... Read More

Advertisements