≡ Menu

PowerShell: How to query date time without seconds

How many times you got a need for querying the date time variable using Get-Date or [DateTime]:Now but without seconds value in it or making it as 00 seconds? Let us find out what are the options to achieve this using PowerShell.

By default Get-Date cmdlet gives seconds as well. In some cases we feel reading till minutes is sufficient or we want to round off to 00 seconds. This is possible if you decide to convert time value to a string as shown below.

$datetime = Get-Date -Format “yyyyMMddHHmm”


While this is good, I was looking for better approaches to do this without actually converting it to a string. Lucky I came across one.

This approach is simple which deducts seconds from current date time value to make it zero.

$datetime = Get-Date



Hope you liked it.


How do we know if a variable of type [DateTime] is in UTC or Local time zone? Let us see how to achieve this using PowerShell.

First you need to ensure that date time information is stored in proper DateTime variable. To check you can invoke GetType() method on the variable that is containing the date time information and it tells us whether it is of date time type or not.

$Date = Get-Date




Once it is confirmed then you can check if the time in the variable is a local time or in UTC timezone by reading the Kind property value. This property has 3 possible values.

  1. Local : The time represented is local time.
  2. UTC : The time represented is UTC.
  3. Unspecified: The time represented is not specified as either local time or Coordinated Universal Time (UTC).

Let us see a quick example:

$Date = Get-Date


$UTCDate = $Date.ToUniversalTime()


A sample screenshot of output is below:


Hope you liked it. Do you know how to find the exact time zone of a date time variable? I will come up with another post for it.


There are several complains around internet that browser links(especially from Google search results) are not working after upgrading iOS to 9.3. This is noticed on my iPhone today with following symptoms.


  1. Right click on results from Google search results in Safari browser not working and hanging the Safari browser
  2. Selecting a link in Google search results not working. The page is not going anywhere.
  3. Same problem is noticed with Google Chrome on iPhone
  4. I have not tested with firefox but there are people complaining similar things with it in forums

If you are also facing similar issues and looking for a fix, then you might want to try disabling Java scripts in Safari browser. This worked for me. One thing I noticed is the richness of google search is lost after disabling the Java scripts. This seems expected since Google runs a lot of Java scripts to provide better user experience.


  • Go to Settings
  • Scroll down to Safari and select it

Safari Advanced Settings

  • Again scroll down till you see Advanced and select it


Safari Settings

  • Now slide the bar again Java Scripts to disable the functionality.


Hope this helps. From the threads in Apple forums I read that Apple is aware of this problem and working on addressing it. Till they come up with a fix, you may want to start using above work around.

Do you know any other better solution or tips for making links in Google search work for Safari browser? Please write in comments section, I am happy to append here.


How to cancel Netflix India subscription

A bit out of topic for this blog, but useful for people who look for unsubscribe option like me. Last month I subscribed to Netflix subscription in INDIA with an excitement But after a month I decided to end the subscription due to some reasons. Personally, I am very happy to see Netflix in INDIA with access to lot of US based movies and TV shows. I am looking forward to see more content coming in.

Initially I was not clear how to unsubscribe. After exploring a bit, I found the easy way.

You just need to visit https://www.netflix.com/CancelPlan and login with your Netflix account. And then you will see a Finish cancellation button like shown in the picture. Click on it and you will see a confirmation message.


The message is clear that my subscription will be active for the current billing cycle as I already paid for it. You can visit http://gadgets.ndtv.com/internet/features/how-to-cancel-your-netflix-subscription-797974 for more options to unsubscribe from Netflix.


C# typeof equivalent in PowerShell

Those who are good with C# knows what typeof does. Basically is used for finding the type data of a given variable or object. How do we do this when it comes to PowerShell? Is there any equivalent keyword for typeof in PowerShell? Let us find out.

PowerShell implements typeof in little different way. Instead of using the typeof keyword like in C#, the PowerShell provides a method called GetType() for each variable or object to find its type. For example, if you want to find data type of a variable called $username, you can know it by calling $username.gettype().

$username = “techibee”


PowerShell Typeof equivalent

You can also check if the returned type is a string, integer or some other data type by comparing it as shown below.

$username = “techibee”

$username.GetType() -eq [string]

$age = 12

$age.GetType() -eq [int32]

$age.GetType() -eq [string]

PowerShell typeof

Do you know any other case where PowerShell’s Gettype() cannot replace typeof keyword in C#? Please post in comments section.


With the increase in use of Windows 10 operating system, you may want to find out how many Windows 10 computers are currently added to your Active Directory Domain. This code snippet will help you to query this information and export it to excel or CSV.

Get-ADComputer cmdlet in ActiveDirectory module is used for querying Computer account details and we can use it to query Windows 10 installations as well. It queries all computers that containing any version of Windows 10 Operating system.

Import-Module ActiveDirectory

Get-ADComputer -Filter { OperatingSystem -like “Windows 10*” } -Properties OperatingSystem

This code needs ActiveDirectory PowerShell  module. You can also try exporting it to CSV by piping the output to Export-CSV cmdlet.

Import-Module ActiveDirectory

Get-ADComputer -Filter { OperatingSystem -like “Windows 10*” } -Properties OperatingSystem | Export-Csv c:\temp\windows10comps.csv -NoTypeInformation

You will see the output recorded in c:\temp\windows10comps.csv file.


Reversing a array using PowerShell

Array is a collection of items. Sometimes we needed to reverse or inverse a array collection. There are many approaches available in PowerShell for doing the reverse or inverse operations but the approach talked in this article is most easiest one.

Reversing String Array using PowerShell

The [Array] accelerator in PowerShell has a static method called reverse() which can be used to reverse a array. This method really doesn’t distinguish whether it is string array, number array or any other kind of array. All it does is reversing it. Though array type is not a matter, I have given a examples for each array type below for better understanding.

$StringArray = @(“abc”,“xyz”,“thn”)




Reversing String Array

Reversing String Array

Reversing numeric array using PowerShell

Below is a example for reversing a numeric array using PowerShell.

$numbericarray = @(1,2,4,3,6,9,7)




Reversing Numeric array

Reversing Numeric array

Reversing Characters array using PowerShell

Below example is for reversing a characters array. Approach is similar to reversing number or string based arrays.

$chararray = @(‘a’,‘c’,‘g’,‘d’,‘r’)





Reversing Char array

Reversing Char array

Hope this article helps.




PowerShell -eq, -ceq and -ieq Comparison Operators

This article helps you in understanding -eq, -ceq, -ieq Comparison operators in Windows PowerShell which is used for comparison purpose. The usage examples of PowerShell -eq operator in this article gives a better understanding of its usage.


Windows PowerShell is one of the great shells that Microsoft ever designed & developed. With help of PowerShell Windows Administrators and developers are benefited a lot as it simplifies the automation in Windows Operating System. You can read more about PowerShell and its uses from TechNet site.

Since the scope of this article is to know more about PowerShell -eq operator, let us start looking at it.

The comparison operator (-eq):

PowerShell is like any other programming language. While developing scripts using PowerShell we need to make use of operators. The -eq (called as equal) operator is one of them. As the name indicates the purpose of this -eq operator is to perform comparison in PowerShell. We can compare strings, numbers and characters using -eq operator. This operator is used for comparing scalar values. The output this -eq operator is a Boolean value(True or False) which indicates whether the input scalar values are equal or not.


Now let us look at some examples to understand how to use this -eq operator in Windows PowerShell for comparison purpose.

String Comparison:

Two strings can be compared using -eq operator in PowerShell to verify if they are equal or not.

“abc” -eq “abc”

“abc” -eq “xyz”


The above code is comparing one string with another and returning True or False based on whether strings are equal or not. You will have to do this many times while writing PowerShell scripts based on your requirements.

Look at the below example where PowerShell -eq operator is used to determine if the user has entered correct PIN number or not.

$UserPIN = Read-Host “Enter your Name”

if($UserPIN -eq “Steve”) {

Write-Host “Welcome Steve!!”

} else {

Write-Host “Sorry $UserPIN, only Steve is allowed”



This -eq operator in PowerShell can accept collections type of variables as well but the use cases of it are less and I don’t want to create confusion by discussing it here.

 Numeric Comparison:

Like the way the -eq operator used for string comparison, it can compare number values as well. Look at the below examples.

100 -eq 100

100 -eq 110

0100 -eq 100

10.1 -eq 10.1

10.2 -eq 10.1


It compared both the operands passed to it and returned the result. You might wonder how it knows if the operands passed to -eq operator are strings or numeric values. That is the beauty of PowerShell. It is capable of understanding the variable type and then perform the comparison.

Also you might have noticed that -eq is capable of comparing decimal number as well.

Character Comparison:

Character comparison in PowerShell is similar to String or numeric comparison. You can compare two characters using -eq operator to verify if they are equal or not.

‘c’ -eq ‘c’

‘c’ -eq ‘z’

See above example for character comparison. If both characters are equal then it returns True otherwise False.


Case-sensitive and Case-insensitive Comparison:

So far all the examples we have seen above are case-insensitive comparisons. Yes, PowerShell -eq operator is designed to perform case insensitive comparison operators. But your PowerShell script requirements might need case sensitive comparisons as well for exact string comparison.

To address such needs, PowerShell has two more operators for string comparison.

  1. Case-Sensitive Comparison (-ceq)
  2. Case-Insensitive Comparison (-ieq)

Case-Sensitive Comparison (-ceq):

This operator works similar to -eq operator but returns false if the strings being compared are different by case. For example, it returns false when you try to compare Windows with windows. Because the first term is having W in capital.

See below example to see how it works:

“Windows” -eq “windows”

“Windows” -ceq “windows”


Case-Insensitive comparison (-ieq):

Even though the -eq operator performs string comparison in case-insensitive way, you still want to ensure a case insensitive comparison, you can use -ieq. The usage of this parameter is very less because most people use -eq which does the same thing. If you know any case where -ieq is preferred over the -eq, please post in the comments of this article so that I will get it added here.

Below is a small example that shows the case-insensitive comparison of strings.

“linux” -eq “Linux”

“linux” -ieq “Linux”

“linux” -ceq “Linux”


The case insensitive compare(-ieq) and the regular compare operator(-eq) returned True in both the cases. But the case sensitive(-ceq) compare returned False.

This case insensitive or sensitive kind of operators are applicable only for Strings and Characters. The numeric values don’t have any case so using -eq operator alone is sufficient.

Do you have any questions about using any of these -eq, -ceq, or -ieq operators? Feel free to post it in comments section below this article.


Get SID of a User account using PowerShell

System administrators want to find SID of user account for troubleshooting purpose and other requirement. In this post let us see how to resolve a user account to SID using PowerShell.

PsGetSid is one of the favorite utility for Windows Server administrators for resolving user names to SID. There are otherways to do this as well. One of the ways is using System.Security.Principal namespace. The script below is based on the classes in this name space. You can check this TechNet page to understand more about its usage using PowerShell.

Save the below code as Get-UserSID.ps1 and then it is ready for usage. Look at the below usage examples.

    [string]$DomainName = $env:USERDOMAIN            
foreach($User in $UserAccount) {            
 $object = New-Object –TypeName PSObject –Prop (            
 $Object.UserName = $User.ToUpper()            
 $Object.DomainName = $DomainName.ToUpper()            
 try {            
     $UserObject = [System.Security.Principal.NTAccount]::new($DomainName,$User)            
     $out = $UserObject.Translate([System.Security.Principal.SecurityIdentifier])            
  $Object.SIDValue = $out.Value            
 } catch {            
  $Object.SIDValue = "FAILED"            

Example#1: Resolve a single user account name to SID

.\Get-UserSID.ps1 -UserAccount testuser21


Example#2: Resolve multiple user accounts to get their SIDs

.\Get-UserSID.ps1 -UserAccount testuser21,testuser22


Example#3: Get SID of a user account from a domain other than current logged on user domain

.\Get-UserSID.ps1 -UserAccount testuser21,testuser1 -DomainName winops.com


This script is a good alternative for psgetsid.exe utility and can be incorporated into any of your scripts to get a user account’s SID details.

Do you have any questions about the usage? Feel free to post it in comments section.


In previous post we have seen how to get SID of current logged on user account using PowerShell. In this post, I will show you how to find if the current logged user account is a local account or a Domain level account.

Related Posts

This previous post we have used UserPrincipal class to get the current logged details. In this post we will use the object returned from this and find out if the user account is local user account or Domain User.

First let us get the details of UserPrincipal into a variable. Don’t forget to load the assembly before accessing AccountManagement namespace.

Add-Type -AssemblyName System.DirectoryServices.AccountManagement            
$UserPrincipal = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current

One of the properties of the returned object is ContextType which contains the details we are looking for. If the value of this property is Domain then the user account is a domain account. The value will be Machine if the account is a local user account.


We can write a small wrapper around this and make a small function that can be reused in any script. This function returns either DomainUser or LocalUser string based on the account type.

Function Get-CurrentUserType {            
#Import Assembly            
Add-Type -AssemblyName System.DirectoryServices.AccountManagement            
$UserPrincipal = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current            
if($UserPrincipal.ContextType -eq "Machine") {            
    return "LocalUser"            
} elseif($UserPrincipal.ContextType -eq "Domain") {            
    return "DomainUser"            

Copy this function into PowerShell window and invoke the function to see the results.

get user account type

Do you have any questions about this approach? Write it in comments section.