Getting the Number of Lines in a String

Getting the Number of Lines in a String

All PowerShell Versions Here is a clever trick how to find out how many lines a string (not a string array!) contains: $text = @' This is some sample text Let's find out the number of lines. '@ $text.Length - $text.Replace("`n",'').Length...

Getting the Number of Lines in a String

Finding Minimum and Maximum Values

All PowerShell Versions To find the smallest and largest item in a range of numbers, use Measure-Object: $list = 1,4,3,1,3,12,990 $result = $list | Measure-Object -Minimum -Maximum $result.Minimum $result.Maximum This works for any input data and any data type. Here...

Getting the Number of Lines in a String

Getting Files with Specific Extensions Only

All PowerShell versions When you use Get-ChildItem to get a list of files, you may have noticed that the -Filter parameter occasionally returns more files than you’ve expected. Here is an example of this. This line does not just return files with a...

DBAs and Dinosaurs

DBAs and Dinosaurs

You have to admit that title is catchier than yet another “Death of the DBA” blog.  And that was exactly the direction I was headed until I ran a quick search to find a reference that not only exposed actual predictions of the death of the DBA as a profession but...

Getting the Number of Lines in a String

Testing UNC Paths

Test-Path can test whether or not a given file or folder exists. This works fine for paths that use a drive letter, but can fail with pure UNC paths. At its simplest, this should return $true, and it does (provided you did not disable your administrative shares):...

Getting the Number of Lines in a String

Exporting and Importing Credentials in PowerShell

Credential objects contain a username and a password. You can create them using Get-Credential, and then supply this object to any cmdlet that has the -Credential parameter. However, what do you do if you want your scripts to run without user intervention yet...

Getting the Number of Lines in a String

Use $PSScriptRoot to Load Resources

Beginning in PowerShell 3.0, there is a new automatic variable available called $PSScriptRoot. This variable previously was only available within modules. It always points to the folder the current script is located in (so it only starts to be useful once you actually...

Getting the Number of Lines in a String

Changing FireMonkey style at runtime

Last month Sarina DuPont blogged about loading custom FireMonkey styles on a mobile device at runtime ("How to load custom styles at runtime"). That's a very interesting approach to compile a custom style as a resource into the mobile app. This week...

Getting the Number of Lines in a String

Getting More Than 1000 Active Directory Results

By default, Active Directory returns only the first 1000 search results when you use an ADSISearcher. This is a security mechanism designed to prevent unspecific LDAP queries from causing domain controller load. If you do need all search results and know that it will...

Getting the Number of Lines in a String

Converting Binary SID to String SID

Converting SID from Binary to String Active Directory accounts contain the SID in binary form. To convert the byte array into a string representation, use a .NET function like this: # get current user $searcher =...

Getting the Number of Lines in a String

Converting Excel CSV to UTF8

When you export Microsoft Excel spreadsheets to CSV files, Excel by default saves CSV files in ANSI encoding. That's bad because special characters will break once you import the data into PowerShell using Import-Csv. Ensuring UTF-8 Encoding for CSV Files To make sure...

Getting the Number of Lines in a String

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...

Getting the Number of Lines in a String

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...

Getting the Number of Lines in a String

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...

Getting the Number of Lines in a String

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...

Getting the Number of Lines in a String

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...

Getting the Number of Lines in a String

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...

Getting the Number of Lines in a String

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...

Getting the Number of Lines in a String

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...