SoFunction
Updated on 2025-03-10

Share tips on using PowerShell

How to modify a Host file using PowerShell

The following command adds a record for the Host file, let the /sps2010/hostheader1/url point to the server itself.

Copy the codeThe code is as follows:

$url=”http://sps2010/hostheader1/”
if( (Get-Content $env:windir\System32\drivers\etc\hosts |?{$_ -imatch "\s$url"}) -eq $null){"`n127.0.0.1 $url" | Out-File -FilePath "$env:windir\System32\drivers\etc\hosts" -Append -encoding ascii}

A PowerShell method call

If I don’t have time to systematically learn PowerShell, I can only learn and use it now. It took me more than half an hour to complete this function call. Haha.
Don't laugh at me, haha.

In this example, some of the following key points in PowerShell are included:

Methods of calling static functions
Specified method of enumeration type
Null value
Boolean value

 
PS C:\Users\> $SPUtility = [] 
PS C:\Users\> $SPPrincipalType = []
PS C:\Users\> $obj=$SPUtility::ResolveWindowsPrincipal($null,$owebapp,"vsat\administrator",$SPPrincipalType::User,$false) 
PS C:\Users\> $obj

PrincipalType   : User 
LoginName     : VSAT\administrator 
IsSharePointGroup : False 
PrincipalId    : -1 
Email       : Administrator@ 
SIPAddress    : 
Mobile      : 
DisplayName    : Administrator 
Department    : 
JobTitle     :

How to determine the current version of PowerShell?

Run the following command:

Get-Host | Select-Object Version

If PowerShell 3.0 is installed, the output should be:

Version
-------
3.0