- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2043 Articles for Microsoft Technologies
1K+ Views
The ValidateScript attribute is to validate the script before entering inside the function. For example, let say you want to validate the path of the file, validate the remote computer connectivity, etc. We will take here remote server connectivity example.Without the ValidateScript attribute, we would have written the script as shown below.Function Check-RemoteServer { param ( [string]$Server ) if(Test-Connection -ComputerName $Server -Count 2 -Quiet -ErrorAction Ignore) { Write-Output "$server is reachable" } else { Write-Output "$Server is unreachable" } }OutputPS C:\> Check-RemoteServer -Server asde.asde asde.asde is ... Read More
330 Views
The ValidateLength attribute in PowerShell is used to validate the length of the String. Generally how we write the command without the mentioned attribute is using the Length method and if/else condition for the string. For Example, Function ValidateStorageName { param ( [String]$StorageName ) if(($StorageName.Length -gt 3) -and ($StorageName.Length -lt 15)) { Write-Output "`nStorage Name validated" } else { Write-Output "`nStorage Name validation failed" } }Output−PS C:\> ValidateStorageName -StorageName Alpha Storage Name validated PS C:\> ValidateStorageName -StorageName CN Storage Name validation failedWith the ValidateLength attribute, else ... Read More
4K+ Views
The ValidateSet attribute in PowerShell function is to validate the values entered from the set which means, it allows only specific values from the set. To understand better consider the below example, we have an array and we need to check if entered values are among array or not then we will use the below code.Function PetAnimalsCheck { param( [String]$Animal ) $Animals = "Cow", "Dog", "Cat", "Horse", "Camel", "Elephant" if($Animals -contains $Animal) { Write-Output "Animal is in the list of Pet Animals" } else { Write-Output ... Read More
983 Views
Validation parameters are a set of rules that you define on the PowerShell variable and restrict users from entering some values and binding users to enter values in the specific domain. If there are not validation parameters the script would be lengthy. The ValidateRange attribute is one of them.ValidateRange AttributeThis parameter is to validate the specific range of numbers. For example, If we need users to enter a value between 5 and 100 and we simply write the script using the If/else statement as shown below.function AgeValidation { param( [int]$age ) if(($age -lt 5) ... Read More
1K+ Views
To search for the word in PowerShell and replace it in the file we will use the string operation. In fact, the Get-Content command in PowerShell is used to read almost any type of file content. In this article, we are considering one text file as shown below.Get-Content C:\Temp\TestFile.txtOutputPS C:\> Get-Content C:\Temp\TestFile.txt # In case of linux, networkInterface names are of the form eth* # In Windows, please use the network full name from Device Manager networkInterfaces: ["Microsoft Hyper-V Network Adapter" ] overrideMetricsUsingScriptFile: false scriptTimeoutInSec: 60 scriptFiles: - osType: windows filePath: monitors/NetworkMonitor/scripts/windows-metrics.bat - osType: unixBase filePath: monitors/NetworkMonitor/scripts/unix-base-metrics.shThe ... Read More
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
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
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
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
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