#powershell -version is not recognized

Note that powershell.exe /? shows the syntax to execute PowerShell from a command prompt. Nevertheless, running

C:\> powershell -noprofile -version 2.0
-version : The term ‘-version’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ -version 2.0
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (-version:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

fails … Read more “#powershell -version is not recognized”

#powershell Select-Object -ExcludeProperty

The ExcludeProperty parameter in Select-Object must always be used together with the Property parameter as illustrated:

gsv | Select-Object -ExcludeProperty status -first 1 | select s*

ServiceName : AJRouter
ServicesDependedOn : {}
ServiceHandle : SafeServiceHandle
Status : Stopped
ServiceType : Win32ShareProcess
StartType : Manual
Site :

gsv | Select-Object -ExcludeProperty status -Property s* -first 1 | select s*

ServiceName : AJRouter
ServicesDependedOn : {}
ServiceHandle : SafeServiceHandle
ServiceType : Win32ShareProcess
StartType : Manual
Site :

#powershell Get-Date v.s. [datetime] Format and System Locale II

Irrespective of the Windows system language and locale, a non-US datetime format text string imported from a file into PowerShell may still need to be formated with Get-Date or cast through [datetime] prior to use.

However, date/time operations may fail silently if the conversion or re-formatting did not succeed. Read more “#powershell Get-Date v.s. [datetime] Format and System Locale II”

#powershell Temporary Files

Introduced in PowerShell 5.0, the New-TemporaryFile cmdlet creates a single ‘scratch’ file with the tmp file extension in a user’s $env:temp directory. For previous #powershell versions, you can do this directly with .NET Framework:

$tempfile = [system.io.path]::GetTempFileName()

Likewise, this has the tmp file extension and is automatically placed in $env:temp. Assigning to a variable permits quick access.

Alternatively, you can create a unique file name for temporary usage by taking advantage of your system date/time as depicted:

$d = Get-Date
$d1 = $d.date.ToShortDateString() -replace “:|/|-|\.”
$d2 = $d.TimeOfDay.ToString() -replace “:|\|-|\.”
$tempfile = “$env:temp\” + $d1 + $d2 + “.tmp”
$tempfile

#powershell Auto Type Conversion – String Array Concatenation

$a1 = “apple”
$a2 = “pear”, “guava”

# add/join 2 string arrays together
[array]$fruits = $a1 + $a2
$fruits
# applepear guava
# $a1 is a simple string type;
# $a2 string array auto expanded and added to same string object

$fruits.count #1 unique string
$fruits.GetType() #a single unit of string array

# one correct way to add/join 2 string arrays together
# force conversion of $a1 simple string to a string array
$fruits = @($a1) + $a2
$fruits
<#
apple
pear
guava
#>

$fruits.count #3 items in array
$fruits.GetType() #an array of strings

$fruits[1]
# pear

#alternative if the order is not important
$fruits = $a2 + $a1
$fruits
<#
pear
guava
apple
#>

$fruits.GetType() #an array of 3 strings

#skype4b Audit Change Tracking Management?

With the right administrative permissions, a #skype4b administrator can add new or modify existing policies and global configuration settings. Every team member is happy when things go according to plan. If a system-wide or scope-level setting[1] goes awry, your end-users will likely be your most reliable “first level alarm” system. In this case, any remedial actions will visibly affect respective services or users in the firm.

Likewise, a Skype for Business enabled user… Read more “#skype4b Audit Change Tracking Management?”

#powershell Get-Date v.s. [datetime] Format and System Locale

When a datetime attribute, say whenChanged in Active Directory gets dump to a text file via Export-Csv, the actual object type stored is that of a string. The format saved is dependent on the system locale. Typically for non en-us western locales, the day precedes the month instead of vice versa.

To convert this back to the proper datetime format through Import-Csv, use… Read more “#powershell Get-Date v.s. [datetime] Format and System Locale”

#powershell Set-SmbShareAccess ?

Grant-SmbShareAccess is the go-to cmdlet to assign and modify permissions granted to trustees on a Windows NT file share as no Set-SmbShareAccess equipvalent exists.

NOTE
*-SmbShareAccess and related cmdlets are available in PowerShell 4 and higher (Module: SmbShare) and are delivered only with operating systems Windows 8 and Windows Server 2012 (and later versions).

#powershell Grant-SmbShareAccess and Security Groups

Unlike net.exe share, Grant-SmbShareAccess can accept one or more security principals for both user accounts and groups (known as “trustee”) against the -AccountName parameter.

Hence instead of:

#requires -version 4.0
#Windows 8 and Windows Server 2012 or higher
Grant-SmbShareAccess `
-Name sharename`
-AccountName `
(Get-LocalGroupMember groupname).name `
-AccessRight Read -Verbose -Confirm:$false

, simply replace the value against -AccountName with groupname.

#powershell Get-Service StartType Property

Commencing with PowerShell v5, the startup type of a conventional Windows NT service is available as the StartType property with a call to the basic Get-Service cmdlet. Ironically, this property going by the name StartupType is already configuarable pre P5 using Set-Service (local administrative permissions needed).

In prior PS versions, a similar attribute StartMode can only be retrieved using Get-WmiObject plus the -Query or -Filter/-Property parameters:

gwmi -query “Select StartMode from Win32_Service Where Name=’wuauserv'”

gwmi -class Win32_Service -Property StartMode -Filter “Name=’wuauserv'”

Alternatively,

&”$env:windir/system32/sc.exe” qc wuauserv | sls START_TYPE

gives the required value under START_TYPE.