Make Parameters Mandatory and Optional at the Same Time

Enabling Visual Styles

When you use Windows Forms dialogs and windows, you may have noticed that they show differently, depending on whether you launched your script from within the ISE editor or from the powershell.exe console. This is because the ISE editor enables visual styles by...

Make Parameters Mandatory and Optional at the Same Time

Test Local User Account Credentials

Verifying a Local User Account Here is a snippet that verifies a local user account. Simply submit a username and a password. You get back either $true or $false: $username = 'Administrator' $password = 'P@ssw0rd' $computer = $env:COMPUTERNAME Add-Type -AssemblyName...

Make Parameters Mandatory and Optional at the Same Time

Using RegEx to Filter Files

Get-ChildItem supports basic wildcards, but it does not support the rich feature set of regular expressions. If you combine Get-ChildItem with Where-Object, you can easily add this functionality. Filtering DLL Files by Name Pattern in PowerShell This example lists all...

Make Parameters Mandatory and Optional at the Same Time

Finding User Account with WMI

WMI represents all kinds of physical and logical entities on your machine. It also has classes that represent user accounts which include both local and domain accounts. Retrieving the Currently Logged-On User This piece of code returns the user account of the...

Make Parameters Mandatory and Optional at the Same Time

Increase SQL Server Performance Using Multiple Files

By default, SQL Server databases are comprised of two files: the primary data file (or .mdf) and the log file (or .ldf). It is, however, possible to configure SQL Server databases to use additional files – which can be an effective means of increasing SQL Server...

Make Parameters Mandatory and Optional at the Same Time

Change Order of CSV Columns

If you have a CSV file and would like to change the order of columns, simply import it into PowerShell, use Select-Object to change the order, and then re-export the CSV file again! $Path = "c:\somepathtocsv.csv" (Import-CSV -Path $Path) | Select-Object -Property...

Make Parameters Mandatory and Optional at the Same Time

Check Windows License Status

In a previous tip we explained how you can use slmgr, a built-in VBScript, to check Windows licensing state. Accessing the Raw Licensing Data in PowerShell The core information used by this VBScript actually comes from WMI, so in PowerShell, you can directly access...

Make Parameters Mandatory and Optional at the Same Time

Tips for Optimizing XML in SQL Server

I’ve worked on a project that used XML heavily inside SQL Server. We really utilized SQL Server’s XML support almost to the full extent, but with some repercussions. As we did our load testing, performance did degrade and we had to step back and adjust how...

Make Parameters Mandatory and Optional at the Same Time

Stripping Decimals Without Rounding

Extracting the Integer Part of a Division Result When you divide numbers and just want the decimals before the decimal point, you could cast the result to integer. However, this would also round the result: PS> 18 / 5 3.6 PS> [Int](18/5) 4 Removing Decimals...

Make Parameters Mandatory and Optional at the Same Time

Removing Multiple White Spaces

Removing multiple white spaces from text is easy in PowerShell. -replace operator Simply use -replace operator and look for whitespaces ("\s") that occur one or more time ("+"), then replace them all with just one whitespace: PS> '[ Man, it works! ]' -replace...

Make Parameters Mandatory and Optional at the Same Time

Sending Emails with Special Characters

Send Emails with PowerShell Using Send-MailMessage PowerShell has built-in support for sending emails: Send-MailMessage! All you need is an SMTP server. However, with standard encoding you may run into issues where special characters are mangled. -Encoding parameter...

Make Parameters Mandatory and Optional at the Same Time

Ignoring Empty Lines

Reading Text Files and Skipping Blank Lines To read in a text file and skip blank lines, try this: $file = 'c:\sometextfile.txt' Get-Content $file | Where-Object { $_.Trim() -ne '' } It will omit empty lines, lines with only blanks and lines with only tabs. ReTweet...

Make Parameters Mandatory and Optional at the Same Time

Writing Registry Key Default Values

Set the default value for a registry key If you need to set the default value for a registry key, you can use either of these approaches: Set-ItemProperty -Path HKCU:\Software\Somekey -Name ‘(Default)’ -Value MyValue Or, you can just do this: Set-Item -Path...

Make Parameters Mandatory and Optional at the Same Time

HTML-Scraping with RegEx

Scraping Website Data with PowerShell To scrape valuable information from websites with PowerShell you can download the HTML code and then use regular expressions to extract what you are after. That's not hard. Here is a sample: $webclient = New-Object...