Found 989 Articles for Software & Coding

What is the difference between $ErrorActionPreference and $ErrorAction cmdlet in PowerShell ?

Chirag Nagrekar
Updated on 03-Sep-2020 10:58:31

778 Views

As we know $ErrorActionPreference and $ErrorAction both have the same functionality and both are used to handle terminating errors by converting Non-Terminating errors to Terminating errors. But when both the variables are used, we need to know which takes precedence.$ErrorActionPreference variable is used at the start of the script while the $erroraction variable is a common parameter and used with the cmdlet. In some cases, we might need the script to be terminated as soon as an error occurs but inside the script, we have some cmdlets which need to be ignored or continued if the error occurs. In that ... Read More

How scriptblock works in PowerShell?

Chirag Nagrekar
Updated on 03-Sep-2020 10:56:07

3K+ Views

Scriptblock is a set of commands which can be executed together when they are invoked. In PowerShell, generally, we write individual commands. Scriptblock can be written with two curly brackets.Example$sb = {Get-Process powershell; Get-Service W32Time}Here we have written two commands under scriptblock. If you directly run this command, scriptblock treats them as a string.PS C:\> $sb Get-Process powershell; Get-Service W32TimeTo run the commands inside the scritblock, use Invoke-Command with the -Scriptblock parameter.Invoke-Command -ScriptBlock $sbOutputHandles NPM(K)    PM(K)    WS(K)    CPU(s)    Id    SI    ProcessName ------- ------    -----    -----    ------    --    --   ... Read More

How to Copy files or Folder without overwriting existing files?

Chirag Nagrekar
Updated on 03-Sep-2020 10:48:21

6K+ Views

To copy files/folders on the remote path without overwriting the existing files/folders, you can use multiple cmdlets like Copy-Item, Robocoy, and Xcopy, etc. As Copy-Item is a standard cmdlet, we will check if it's supported parameters can prevent overwriting.If Copy-Item doesn’t work then we will check its alternate command. Copy-Item simply overwrites the files and folders on the destination path and the copies newer files.For example, To copy files from the source folder C:\Test1 to the destination folder C:\Test2 below command is used and it simply overwrites the file without asking.ExampleCopy-Item C:\Test1\* C:\Test2 -Recurse -VerboseOutputPS C:\Temp> Copy-Item C:\Test1\* C:\Test2 -Recurse ... Read More

How to run PowerShell commands in Background?

Chirag Nagrekar
Updated on 03-Sep-2020 10:42:36

13K+ Views

To run commands in the background in the PowerShell, you need to use Background job cmdlets. Background job means running commands/job in the background without occupying the console.Start-Job is one of the job scheduler cmdlets for PowerShell which runs PowerShell commands in the background without interacting with the current user session as a Job so that users can work in the PowerShell console without losing the control of the console while the command is running in the background.When PowerShell's job starts using Start-Job, a job returns the object immediately even if the job takes an extended time.Start-Job is designed to ... Read More

How to remove windows features using PowerShell?

Chirag Nagrekar
Updated on 26-Aug-2020 08:25:27

3K+ Views

To remove windows features, we can use command Remove-WindowsFeature command is used along with feature name.Remove-WindowsFeature Search-Service -VerboseVERBOSE: Uninstallation started... VERBOSE: Continue with removal? VERBOSE: Prerequisite processing started... VERBOSE: Prerequisite processing succeeded. Success Restart Needed Exit Code Feature Result ------- -------------- --------- -------------- True No Success {Windows Search Service} VERBOSE: Uninstallation succeeded.If the windows feature has the management tools like as in Web-Server (IIS) feature, you can add the same in the command line. If the server requires restart then you can add the -Restart parameter. For example, Remove-WindowsFeature Web-Server -IncludeManagementTools -Restart -VerboseIf we check the -Name parameter, it supports ... Read More

How to Install Windows Features with PowerShell?

Chirag Nagrekar
Updated on 26-Aug-2020 08:23:44

5K+ Views

To install windows features on the server, Install-WindowsFeature cmdlet is used .Install-WindowsFeature Windows-Server-Backup -LogPath C:\Temp\Installfeatures.txt -VerboseIn the above example, Windows-Server-Backup feature will be installed on the local server and logs will be stored at location C:\Temp and file name InstallFeatures.txt.PS C:\Users\Administrator> Install-WindowsFeature Windows-Server-Backup -LogPath C:\Temp\Installfeatures.txt -Verbose VERBOSE: Installation started... VERBOSE: Continue with installation? VERBOSE: Prerequisite processing started... VERBOSE: Prerequisite processing succeeded. Success Restart Needed Exit Code Feature Result ------- -------------- --------- -------------- True No Success {Windows Server Backup} VERBOSE: Installation succeeded.You can also install the feature with the pipeline command, Get-WindowsFeature Windows-server-backup | Install-WindowsFeature -LogPath C:\Temp\Installfeatures.txt -VerboseIf your windows feature ... Read More

How to Get Windows features using PowerShell?

Chirag Nagrekar
Updated on 26-Aug-2020 08:19:14

10K+ Views

To get the windows features and roles available or installed using PowerShell, you need to use the Get-WIndowsFeature cmdlet. That is obvious that Windows features and roles are available only on the server operating systems not on the client operating system.When you run the Get-WindowsFeature on the server operating system from Windows server 2008 onwards using PowerShell, you will get the output as below.The crossed symbol in the square box indicates that the feature is installed. You can also check the same using ‘Install State’. To get only Installed features on the server, you need to filter out the Install ... Read More

How to uninstall software using Package management in PowerShell?

Chirag Nagrekar
Updated on 08-Aug-2020 07:45:00

3K+ Views

There are mainly 3 methods by which you can uninstall software using PowerShell.WMI Method.Using Package providerUninstallation String.Here, we will discuss the method to uninstall software using Package management.You can uninstall the software or packages which are installed with the package providers. You can get the list of the package providers using Get-PackageProvider command.PS C:\Users\Administrator> Get-PackageProvider | Select Name, Version Name          Version ----          ------- msi           3.0.0.0 msu           3.0.0.0 PowerShellGet 1.0.0.1 Programs      3.0.0.0So the packages which are installed with msi, msu, Programs ... Read More

How to uninstall software using WMI in PowerShell?

Chirag Nagrekar
Updated on 08-Aug-2020 07:42:21

2K+ Views

There are mainly 3 methods by which you can uninstall software using PowerShell.WMI Method.Using Package providerUninstallation String.We will discuss here the WMI method to uninstall software.WMI methodWith WMI class Win32_Product you can retrieve the list of software uninstalled in your local or the remote systems. If you need specific software, you can filter by its name. For example, Get-WmiObject Win32_Product -Filter "Name='Vmware tools'"Or You can retrieve the name of the installed software using the Where-Object pipeline command.Get-WmiObject Win32_Product | Where{$_.Name -eq "Vmware tools"}OutputPS C:\Users\Administrator> Get-WmiObject Win32_Product | Where{$_.Name -eq "Vmware tools"} IdentifyingNumber : {D533345C-7F8D-4807-AE80-E06CE2045B0E} Name           ... Read More

Explain PowerShell Profile.

Chirag Nagrekar
Updated on 08-Aug-2020 07:39:52

405 Views

When you open PowerShell, it loads the profile just like the Windows operating system. When you log in to windows OS you are logged into your profile and every user has their individual profile. It is called the current profile for the current host.To check your profile, type $Profile command in the PowerShell console.PS C:\Users\Administrator> $profile C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.p s1This was for Powershell console but let's check if Powershell uses the same profile for ISE.PS C:\> $profile C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profil e.ps1So the ISE has its own profile too and both are stored in the $Home directory. What if we use the $profile for VSCode.PS ... Read More

Advertisements