1909-powershell-datetime
1909-powershell-datetime¶
overview¶
- datetime object on powershell
jupyter notebook¶
In [23]:
Get-Date
In [24]:
Get-Date -format "yy/MM/dd hh:mm"
In [25]:
Get-Date -format "yyMMdd_hhmm"
Get-Help¶
In [26]:
Get-Help Get-Date
In [27]:
Get-Help Get-Date -online
Get-Command and Get-Alias¶
In [29]:
Get-Command -Noun Date
Identification of Type¶
In [30]:
(Get-Date) -is [datetime]
In [32]:
(Get-Date).GetType().Fullname
Get-Member¶
In [33]:
(Get-Date) | Get-Member -MemberType Property | Format-Table Name, Definition
In [34]:
(Get-Date) | Get-Member -MemberType Method | Format-Table Name, Definition
Methods¶
calculate¶
In [37]:
date -> str (formatting)¶
In [39]:
@("d","D","f","F","g","G","M","O","R","s","t","T","u","U","Y") |
foreach {$date=Get-Date -F $_ ;"$_, $date"}
In [41]:
Get-Date -F 'yyyy/MM/dd HH:mm:ss.ffffff'
In [43]:
(Get-Date -F O).Replace('-','').Replace('T','_').Replace(':','').Replace('+','_GMT+')
str -> datetime¶
In [45]:
Get-Date '2019/09/01'
In [47]:
Get-Date '2019/08/28 22:12'
serialization¶
In [52]:
Get-Date | Export-Clixml date.clixml
In [54]:
cat date.clixml
timespan¶
In [62]:
Get-Command -Noun Time*
In [64]:
Get-Help New-Timespan
In [65]:
Get-Help New-Timespan -online
Get-Member¶
In [66]:
$timespn = New-Timespan
In [74]:
$timespn | Get-Member -MemberType Property | ft Name, Definition
In [76]:
$timespn | Get-Member -MemberType Method | ft Name, Definition
In [78]:
In [80]:
$span | format-list
In [82]:
'{0:g}' -f $span
In [84]:
$str = '{0:g}' -f $span
$str.Substring(0, $str.length -3)
In [86]:
reference¶
- 【PowerShell】TimeSpanオブジェクトを作成する
- Use PowerShell and Conditional Formatting to Format Time Spans
- PowerShellのGet-Date -Formatで出力される標準の書式指定結果一覧
</div>
1909-powershell-alias
1909-powershell-alias¶
overview¶
- overview of alias on powershell
jupyter notebook¶
contents¶
Basic¶
In [10]:
In [122]:
dir | set -Name dirs
$dirs[0..10] + "`r`n and more ...."
In [4]:
ls index.*
In [6]:
cat README.md
In [8]:
type .gitignore
Get-Help¶
In [12]:
Get-Help Get-Alias
In [111]:
Help about_alias | set -Name store
$store[0..15] + "`r`n and more ...."
Get-Commands¶
In [47]:
Get-Command -Noun Alias
Alias¶
In [114]:
Get-Alias | set -Name ary
$ary[0..15] + "`r`n and more ...."
In [79]:
Get-Alias | Out-String -Stream | select-string 'Content'
In [94]:
Get-Alias | Out-String -Stream | sls 'process'
In [81]:
Get-Alias -Definition '*Item'
In [29]:
Get-Alias | Where-Object {$_.Name -eq 'cat'} | Format-List *
In [39]:
Get-Alias | Where-Object {$_.Definition -match 'Content'} | Sort-Object Definition | Format-Table Definition, Name, HelpUri
In [41]:
Get-Alias | Where-Object {$_.Definition -match 'Item$'} | Sort-Object Definition | Format-Table Definition, Name, HelpUri
In [86]:
Get-Alias -Definition '*Location' | Sort-Object Definition | Format-Table Definition, Name, HelpUri
In [91]:
gal -Def '*Path' | sort Definition | ft Definition, Name, HelpUri
In [51]:
Get-Alias | Where {$_.Definition -match 'Object$'} | Sort Definition | Select Definition, Name, HelpUri
In [59]:
Get-Alias | Where {$_.Definition -match '^Format'} | Sort Definition | Select Definition, Name, HelpUri | format-table *
Frequenctly Using¶
In [63]:
$ary_for_frequent_use_obj = @(
'where','?','foreach','%','select','sort', 'group','measure','tee'
)
Get-Alias | Where {$_.Name -in $ary_for_frequent_use_obj} | Sort Definition | Select Definition, Name, HelpUri
In [73]:
$ary_for_frequent_cmd = @(
'man', 'help', 'echo','write','cat','type','pwd','dir','ls','cd','chdir','clear','copy','cp', 'del','move','mv','ren','rm','rmdir', 'del', 'set'
)
Get-Alias | Where {$_.Name -in $ary_for_frequent_cmd} | Sort Definition | Select Definition, Name, HelpUri
In [75]:
In [77]:
$ary_other_rare = @(
'sls', 'fl', 'ft','gm','gal'
)
Get-Alias | Where {$_.Name -in $ary_other_rare} | Sort Definition | Select Definition, Name, HelpUri
Appendix¶
In [116]:
Get-Alias
In [124]:
Help about_alias
In [ ]:
</div>
1908-powershell-hashtable
1908-powershell-hashtable¶
overview¶
- hashtable object on powershell
jupyter notebook¶
Usage¶
basic¶
In [1]:
$d = @{
'script' = 'powershell'
'os' = 'windows 10'
'date' = Get-Date
}
In [2]:
$d
In [3]:
$d['script']
In [4]:
$d.GetType()
In [5]:
$d['os'].GetType()
In [6]:
$d['date'].GetType()
Get-Help¶
In [7]:
Get-Help -Category HelpFile | where-object {$_.Name -match "hash"}
In [72]:
Get-Help about_Hash* | set -Name store
$store -split "`r`n" | set -Name ary_store
$ary_store[0..25] + 'and more .....'
Initialize¶
Empty object¶
In [74]:
$dic = @{}
truthiness and falsiness¶
In [10]:
if($dic){'true'}else{'false'}
In [11]:
if($dic.Count){'true'}else{'false'}
In [12]:
$dic.Count
Get-Member¶
In [78]:
$dic | Get-Member -MemberType Property | select Name, Definition
In [14]:
$dic | Get-Member -MemberType Method | Format-Table Name, Definition
Methods¶
In [15]:
$dic = @{}
$dic | Get-Member -MemberType Method | foreach-object { $_.Name}
Add¶
In [18]:
$dic = @{}
In [19]:
$dic.Add('one', 1)
$dic.Add('Two', 2)
$dic.Add('Three', 3)
In [20]:
$dic
foreach¶
In [21]:
foreach($itm in $dic.GetEnumerator())
{
echo $itm.Key $itm.Value
}
In [22]:
foreach($key in $dic.Keys)
{
echo $Key ($dic[$key] + 10)
}
contains¶
In [23]:
$dic
In [24]:
$dic.Contains('one')
In [25]:
$dic.Contains('four')
In [26]:
$dic.ContainsValue(4)
In [27]:
$dic.ContainsValue(2)
Remove and Clear¶
In [28]:
$dic
In [29]:
$dic.Remove('Two')
In [30]:
$dic
In [31]:
$dic.Remove('Four')
In [32]:
$dic
In [33]:
$dic.Clear()
In [34]:
echo "dic = $dic"
In [35]:
echo "dic---" $dic "+++"
Clone¶
In [36]:
$dic_org = @{
'first' = '1st'
'second' = '2nd'
'third' = '3rd'
'fourth' = '4th'
}
In [37]:
$dic_org
In [38]:
$dic_dist = $dic_org
In [39]:
$dic_dist['first'] = '1st step'
In [40]:
$dic_dist
In [41]:
$dic_org
In [42]:
$dic_dist_deep = $dic_org.Clone()
$dic_dist_deep['first'] = '1st'
In [43]:
$dic_dist_deep['first']
In [44]:
$dic_org['first']
hash -> string¶
In [45]:
$dic_for_conv = @{
'one' = 1
'two' = 2
'three' = 3
}
In [46]:
$dic_for_conv
In [47]:
write-host $dic_for_conv
In [48]:
echo "dic_for_conv = $dic_for_conv"
In [49]:
echo "dic_for_conv = " $dic_for_conv
In [50]:
$dic_for_conv.ToString()
In [51]:
$dic_for_conv | Convertto-Csv
- Convertto-Csv
In [52]:
$dic_for_conv | Convertto-Csv -NoTypeInformation
In [53]:
$dic_for_conv | Get-Member -MemberType Property
In [54]:
$dic_for_conv | Convertto-Json
Convertto-Json
hash -> psobj¶
In [56]:
$jsn = $dic_for_conv | Convertto-Json -Compress
In [57]:
echo "dic_for_conv Json = $jsn"
In [58]:
$psobj = $jsn | ConvertFrom-Json
$psobj
In [59]:
$psobj.ToString()
In [60]:
$psobj | Convertto-Csv -NoType
In [61]:
$psobj | Convertto-Json -Compre
In [84]:
$psobj2
Serialize¶
In [62]:
$dic_for_serialized = @{
'one' = '1st'
'two' = '2nd'
'three' = '3rd'
}
In [63]:
$dic_for_serialized | Export-Clixml dic_for_serialized.clixml
In [64]:
In [65]:
dir $pwd/*.clixml
In [66]:
cat dic_for_serialized.clixml
In [67]:
$dic_for_deserialized = Import-Clixml dic_for_serialized.clixml
In [68]:
$dic_for_deserialized
appendix¶
In [69]:
Get-Help about_Hash_Tables
In [ ]:
</div>
1908-powershell-array
1908-powershell-array¶
jupyter notebook¶
変数としてのarray の定義¶
Literalによる定義¶
In [1]:
$ary = @('power','shell')
In [2]:
$ary
In [3]:
$ary_empty = @()
空の配列
In [4]:
$ary_empty
In [5]:
$ary_other_def = 'power', 'shell', 'other', 'array'
In [6]:
$ary_other_def
$ary.GetType()¶
In [7]:
$ary.GetType() | Format-List
$ary | Get-Member¶
In [28]:
$ary | Get-Member | format-table Name, Definition
In [10]:
Get-Member -InputObject $ary -MemberType Method | format-table Name, Definition
In [11]:
$ary.Count
In [12]:
$ary.ToString()
In [13]:
$ary.Add('Jupyter')
In [14]:
$ary += 'Jupyter'
In [15]:
Get-Member -InputObject $ary -MemberType Property | format-table Name, Definition
In [16]:
$ary
In [34]:
$ary | out-Default
In [17]:
$ary | foreach-object {$_.GetType().FullName; '>>' + $_}
In [18]:
foreach($ar in $ary)
{
$ar.GetType().FullName
'>>' + $ar
}
In [19]:
$ary.ToString()
In [20]:
$ary | ConvertTo-Csv
In [21]:
$ary | ConvertTo-Csv -NoTypeInformation
In [22]:
ConvertTo-Csv -inputObject $ary -NoTypeInformation
In [23]:
ConvertTo-Json -inputObject $ary
In [24]:
$ary | Convertto-Json
In [25]:
$ary -join ','
In [26]:
Get-Help about_array
reference¶
- How do I convert an array object to a string in PowerShell?
In [ ]:
</div>
1908-powershell-alias-grep
1908-powershell-alias-grep
overview
- powershellのAliasを、grepする
notebook
log
$PSVersionTable get-alias | out-string -stream | select-string "new-item" get-alias | out-string -stream | select-string "Out" get-alias | out-string -stream | select-string "get-alias" gal | out-string -stream | sls 'sls' gal | oss | sls 'dir'
1908-powershell-specialfolder-getfolderpath
1908-powershell-specialfolder-getfolderpath
nbviewer
code
[Enum]::GetNames('System.Environment+SpecialFolder') $mydocfolder = [Environment]::GetFolderPath('MyDocuments') $mydocfolder $mydocfolder.GetType().FullName [Environment]::GetFolderPath('ApplicationData') [Environment]::GetFolderPath('Startup')
1908-powershell-split-path
1908-powershell-split-path
overview
- powershellのsplit-pathのサンプルコード
nbviewer
conde
$PSVersionTable $PROFILE $path = split-path $PROFILE -parent $path explorer $path split-path $PROFILE -leaf sakura $PROFILE