0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellでファイル情報を取得

Posted at

今まで、Windowsのファイル情報をVB6系(VBS)で取得していました。
そろそろVBSが無くなりそうな感じなので、
PowerShellでファイル情報を取得したかったので、やってみた。

ps1ファイルの内容 :


# 「$result」にファイルパスを指定
$path = "C:\Users\(who)\Desktop\test.txt"

function getFileInformations([string]$inFullPathFile){
    $path = $inFullPathFile
    $shell = New-Object -COMObject Shell.Application

    # フォルダとファイル名を変数に設定
    $folder = Split-Path $path
    $file = Split-Path $path -Leaf

    $Getfolder = $shell.Namespace($folder)
    $Getfile = $Getfolder.ParseName($file)

    $gotDirectoryName = ""
    $gotName = ""
    $gotLastWriteTime = ""
    $gotLength = ""
    $gotHash = ""

    # ファイルが存在するか確認
    if((Test-Path $path) -eq "True"){
        #ファイルが存在する
        $gotDirectoryName = (Get-ItemProperty $path).DirectoryName
        $gotName = (Get-ItemProperty $path).Name
        $gotLastWriteTime = (Get-ItemProperty $path).LastWriteTime
        $gotLength = (Get-ItemProperty $path).Length

        #MD5のハッシュ値
        $gotHash = (Get-FileHash $path -Algorithm md5).Hash.toLower()
    }

    return $gotDirectoryName , $gotName , $gotLastWriteTime , $gotLength , $gotHash
}

$fileInfo = getFileInformations($path)

Write-host "チェック対象ファイル" = $path
Write-host "取得した項目数" = $fileInfo.Length
Write-host "0 . DirectoryName" = $fileInfo[0]
Write-host "1 . Name" = $fileInfo[1]
Write-host "2 . LastWriteTime" = $fileInfo[2]
Write-host "3 . Length" = $fileInfo[3]
Write-host "4 . gotHash" = $fileInfo[4]

実行結果 :


PS C:\Users\(who)> C:\Users\(who)\Desktop\test.txt
チェック対象ファイル = C:\Users\(who)\Desktop\test.txt
取得した項目数 = 5
0 . DirectoryName = C:\Users\(who)\Desktop
1 . Name = test.txt
2 . LastWriteTime = 2025/01/04 23:52:54
3 . Length = 1713
4 . gotHash = e3b4c930886dcd81d978c7249b965f7a
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?