8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PowerShell】ファイル・フォルダの存在を確認する方法

Posted at

はじめに

PowerShellで、ファイル・フォルダの存在を確認する方法について今回はアウトプットします。
ファイル等の存在の判定が必要になるときに便利です。

PowerShellスクリプト実行環境

  • OS

    Microsoft Windows 10 Pro
OS情報
PS C:\Users\owner> (Get-WmiObject Win32_OperatingSystem).Caption
Microsoft Windows 10 Pro
  • PowerShellバージョン

    5.1.18362.752
PowerShellバージョン
PS C:\Users\owner>  $PSVersionTable

Name                           Value                                                                                                                                                                                                                                                
----                           -----                                                                                                                                                                                                                                                
PSVersion                      5.1.18362.752                                                                                                                                                                                                                                        
PSEdition                      Desktop                                                                                                                                                                                                                                              
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                                              
BuildVersion                   10.0.18362.752                                                                                                                                                                                                                                       
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                                      
WSManStackVersion              3.0                                                                                                                                                                                                                                                  
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                                                  
SerializationVersion           1.1.0.1                                                                                                                                                                                                                                              

使用コマンド

使用コマンドはこちらになります。

使用コマンド
Test-Path <ファイル名>

ファイル/フォルダ/レジストリの確認で使用することが可能

使用例

ファイル/フォルダ/レジストリの確認でそれぞれ使用した場合の例を紹介します。

ファイル単体の場合

2.jpg

  • C:\Users\owner\Desktop\tmp\test.txt(テキストファイル)を確認したい場合
実行例
PS C:\Users\owner> Test-Path C:\Users\owner\Desktop\tmp\test.txt
True
  • ファイル名を誤った場合
実行例
PS C:\Users\owner> Test-Path C:\Users\owner\Desktop\tmp\test.tx
False

フォルダの場合

3.jpg

  • C:\Users\owner\Desktop\tmp(フォルダ)を確認したい場合
実行例
PS C:\Users\owner> Test-Path C:\Users\owner\Desktop\tmp
True
  • フォルダ名を誤った場合
実行例
PS C:\Users\owner> Test-Path C:\Users\owner\Desktop\tm
False

レジストリの場合

4.JPG

  • HKLM:\SOFTWARE\7-Zip(レジストリ)を確認したい場合
実行例
PS C:\Users\owner> Test-Path HKLM:\SOFTWARE\7-Zip
True
  • レジストリ名を誤った場合
実行例
PS C:\Users\owner> Test-Path HKLM:\SOFTWARE\7-Zi
False

ファイルの存在判定をしてみる

C:\Users\owner\Desktop\tmp\test.txt(テキストファイル)が存在するか判定するスクリプトになります。

test.ps1
# 「$result」にファイルパスを指定
$dir = "C:\Users\owner\Desktop\tmp\test.txt"

# ファイルが存在するか確認
if((Test-Path $dir) -eq "True"){
  Write-Host "True"
}else{
  Write-Host "False"
}
実行結果
PS C:\Users\owner> C:\Users\owner\Desktop\scripts\test.ps1
False

参考リンク

Test-Pathコマンドレットを使用してファイル・フォルダの存在確認を行う方法

8
8
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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?