In PowerShell prompt is a function
$dir function:
CommandType Name Definition
----------- ---- ----------
Function prompt $
Function TabExpansion ...
Function Clear-Host $spaceType
Function more param([string[]]$paths);
Function help param([string]$help);
Function man param([string]$help);
Function mkdir param([string[]]$paths);
Function md param([string[]]$paths);
Simple prompt in PowerShell
function prompt { "$"}
CMD equivalent: PROMPT $$
Bash equivalent: PS1="$"
Most common prompt which displays your current working directory
function prompt { "$(get-location)> " }
CMD : PROMPT $P$G
Bash : PS1='\w$' or PS1='`pwd`'
Another prompt to display current working directory ( not the full path )
function prompt { "$(split-path $(get-locaiton) -leaf)> "
CMD : ????
Bash : PS1='\W $'
Prompt with linefeed character
function prompt { "$(get-location)`n> "}
CMD : $P$_$G
Bash : PS1='\w\n>'
Prompt with DateTime
function prompt { "$(get-date)>"}
CMD : $T$G
Bash : PS1='\t>'
Bash has four special codes for representing datetime prompt string
\t : 17:05:05
\T : 05:05:05
\@ : 05:05 PM
\d : Sun Aug, 06
Let's do samething in powershell
"$($(get-date).tostring('HH:MM:s'))" ==> 17:05:05
"$($(get-date).tostring('hh:MM:s'))" ==> 05:05:05
"$($(get-date).tostring('hh:MM tt'))" ==> 05:05 PM
"$($(get-date).tostring('ddd MMM, dd'))" ==> Sun Aug, 06
Prompt with Hostname
function prompt { "$env:computername > "}
CMD: prompt %computername%$G
Bash : PS1='\h>'
NOTE: Bash also offers \H which will give you the complete hostname in the domain, you can mimic the same thing in powershell using following prompt
function prompt { "$([System.Net.Dns]::GetHostByName([System.Net.Dns]::GetHostName()).hostname) >" }
Prompt with Username
function prompt { "$env:username > "}
CMD: prompt %username%$G
Bash : PS1='\u>'
-
Get-WmiObject -Namespace ROOT -Query 'SELECT * FROM __Namespace' | Select Name
$vSmsvc = Get-WmiObject -Namespace ROOT\Virtualization -Class Msvm_VirtualSwitchManagementService
Get-WmiObject -Namespace ROOT\Virtualization -List | ? { $_.Name -match '^Msvm' }3View comments
-
Function Load-EmlFile
{
Param
(
$EmlFileName
)
$AdoDbStream = New-Object -ComObject ADODB.Stream
$AdoDbStream.Open()
$AdoDbStream.LoadFromFile($EmlFileName)
$CdoMessage = New-Object -ComObject CDO.Message
$CdoMessage.DataSource.OpenObject($stream,"_Stream")
return $CdoMessage
}3View comments
-
new-item -path $profile -type file -force
'$MaximumHistoryCount=9999' | out-file $profile
"`n" | out-file $profile -append
'Start-Transcript' | out-file $profile -append1View comments
-
[Reflection.Assembly]::LoadWithPartialName("System.Configuration")
$config=[System.Configuration.ConfigurationManager]::OpenExeConfiguration([System.Configuration.ConfigurationUserLevel]::None)
$config.SectionGroups | % { $_.name }
$config.SectionGroups.Item("system.serviceModel").Bindings.NetTcpBinding.Bindings0Add a comment
-
"$port=2020
$endpoint = new-object System.Net.IPEndPoint ([IPAddress]::Loopback,$port)
$udpclient=new-Object System.Net.Sockets.UdpClient
$b=[Text.Encoding]::ASCII.GetBytes('Is anyone there?')
$bytesSent=$udpclient.Send($b,$b.length,$endpoint)
$udpclient.Close()
$port=2020
$endpoint = new-object System.Net.IPEndPoint ([IPAddress]::Any,$port)
$udpclient=new-Object System.Net.Sockets.UdpClient $port
$content=$udpclient.Receive([ref]$endpoint)
[Text.Encoding]::ASCII.GetString($content)"1View comments
-
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
$caption = "Warning!"
$message = "Do you want to proceed"
$result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
if($result -eq 0) { Write-Host "You answered YES"
if($result -eq 1) { Write-Host "You answered NO" }4View comments
-
powershell -c set-executionPolicy unrestricted -scope localMachine –force0
Add a comment
-
DirectorySearcher gets initializes with default domain controller.
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.Filter = '(&(objectClass=User)(displayName=fistName lastName))'
$userResult = $searcher.FindOne()3View comments
Add a comment