インストールされているモジュールをインストールする方法
PS> Install-Module -Name <モジュール名>
インストールされているモジュールを表示
PS> Get-Module
モジュールをインポートする方法
PS> Import-Module <module-name>
モジュールを削除する方法
PS> Remove-Module <module-name>
cd D:\script
$TargetFile = "D:\script\wrk\production\dat\product\product_20201001\wiki-001.zip"
D:\script\wrk\deveroper\dat\product\product_20201001\wiki
"D:\script\bin\7z.exe" e -y -o
$filelist = (Get-ChildItem D:\script\wrk\production\dat\ -Recurse | ? { ! $_.PSIsContainer }).fullname
$TargetFile = New-Object PSObject | Select-Object drive, folder1, folder2, folder3, folder4, folder5, folder6, file
$Data = @()
foreach ( $file in $filelist ) {
$TargetFile = ($file).split("\")
$Data += $TargetFile
echo $TargetFile
echo ---
}
$Data
関数:
function Global:Get-DirSize() {
Param( [string]$TargetDir )
if ( $TargetDir.Length -eq 0 ) {
$Host.UI.WriteErrorLine( "Usage: Get-DirSize <path>" )
return
}
write-output ( "---------------------------------------------------------------------------------------------------" )
write-output ( "TargetDirectory : ${TargetDir}" )
write-output ( "---------------------------------------------------------------------------------------------------" )
set-location ${TargetDir}
Get-ChildItem | Select-Object FullName, @{ name = "Size(MB)"; expression = { [math]::round((Get-ChildItem $_.FullName -Recurse -Force | Measure-Object Length -Sum ).Sum /1MB ) } }
}
関数実行: PS C:\> Get-DirSize C:\
実行結果: --------------------------------------------------------------------------------------------------- TargetDirectory : C:\ --------------------------------------------------------------------------------------------------- FullName Size(MB) -------- -------- C:\Autodesk 4997 C:\BUFFALO 101 C:\ESD 0 C:\Intel 0 C:\PerfLogs 0 C:\Program Files 3256 C:\Program Fi... 7851 C:\Users 257541 C:\VNTApp 0 C:\Windows 27189
PS C:\>
#####################################################################
# システム情報
#####################################################################
function QueryServerInfo(){
$ReturnData = New-Object PSObject | Select-Object HostName,Manufacturer,Model,SN,CPUName,PhysicalCores,Sockets,MemorySize,DiskInfos,OS
$Win32_BIOS = Get-WmiObject Win32_BIOS
$Win32_Processor = Get-WmiObject Win32_Processor
$Win32_ComputerSystem = Get-WmiObject Win32_ComputerSystem
$Win32_OperatingSystem = Get-WmiObject Win32_OperatingSystem
# ホスト名
$ReturnData.HostName = hostname
# メーカー名
$ReturnData.Manufacturer = $Win32_BIOS.Manufacturer
# モデル名
$ReturnData.Model = $Win32_ComputerSystem.Model
# シリアル番号
$ReturnData.SN = $Win32_BIOS.SerialNumber
# CPU 名
$ReturnData.CPUName = @($Win32_Processor.Name)[0]
# 物理コア数
$PhysicalCores = 0
$Win32_Processor.NumberOfCores | % { $PhysicalCores += $_}
$ReturnData.PhysicalCores = $PhysicalCores
# ソケット数
$ReturnData.Sockets = $Win32_ComputerSystem.NumberOfProcessors
# メモリーサイズ(GB)
$Total = 0
Get-WmiObject -Class Win32_PhysicalMemory | % {$Total += $_.Capacity}
$ReturnData.MemorySize = [int]($Total/1GB)
# ディスク情報
[array]$DiskDrives = Get-WmiObject Win32_DiskDrive | ? {$_.Caption -notmatch "Msft"} | sort Index
$DiskInfos = @()
foreach( $DiskDrive in $DiskDrives ){
$DiskInfo = New-Object PSObject | Select-Object Index, DiskSize
$DiskInfo.Index = $DiskDrive.Index # ディスク番号
$DiskInfo.DiskSize = [int]($DiskDrive.Size/1GB) # ディスクサイズ(GB)
$DiskInfos += $DiskInfo
}
$ReturnData.DiskInfos = $DiskInfos
# OS
$OS = $Win32_OperatingSystem.Caption
$SP = $Win32_OperatingSystem.ServicePackMajorVersion
if( $SP -ne 0 ){ $OS += "SP" + $SP }
$ReturnData.OS = $OS
return $ReturnData
}
powercfg /batteryreport
| 項目 | 説明 |
| Installed batteries | バッテリー状態 |
| Recent usage | 最近の使用状態(過去3日間) |
| Battery usage | バッテリーの使用状態(過去3日間の電池残量) |
| Usage history | 使用状態履歴 |
| Battery capacity history | バッテリーキャパシティ履歴 |
| Battery life estimates | 使用状況から推測したバッテリー稼働可能時間 |
System.Collections.Hashtableのインスタンスを作成
$RadioButtons = @{}
PS C:> $RadioButtons = @{
1 = "AAA";
2 = "BBB";
3 = "CCC";
}
PS C:> for($i=0; $i -le 3; $i++) {
$RadioButtons.${i} = "AAA${i}"
}
PS C:> $RadioButton = ""
PS C:> foreach ($RadioButton in $RadioButtons) {
$RadioButton
}
Name Value
---- -----
3 AAA3
2 AAA2
1 AAA1
0 AAA0
foreachでループ処理するとき。ValuesプロパティにValueのコレクション、KeysプロパティにKeyのコレクションが入っている。
PS C:> $RadioButtons.Values | foreach { $_ }
AAA3
AAA2
AAA1
AAA0
Valueを連想配列にして、値を取得する例。
PS C:> $dialog1 = @{
"Text" = "Dialog1";
"Size" = @{
"Width" = 600;
"Height" = 400;
};
}
PS C:> $dialog1.Size.Width 600
PS C:> $dialog1.Values | foreach { $_ }
Dialog1
Name Value
---- -----
Height 400
Width 600
PS C:> $dialog1.Keys | foreach { $_ }
Text
Size