https://github.com/sangafabrice/windows-path-type
The WindowsPath type is an alternative to the Test-Path cmdlet bound with the parameter IsValid on the Windows Platform. It excludes file path strings that contain invalid characters.
https://github.com/sangafabrice/windows-path-type
ci-cd-pipeline modules psgallery workflow
Last synced: 10 months ago
JSON representation
The WindowsPath type is an alternative to the Test-Path cmdlet bound with the parameter IsValid on the Windows Platform. It excludes file path strings that contain invalid characters.
- Host: GitHub
- URL: https://github.com/sangafabrice/windows-path-type
- Owner: sangafabrice
- License: apache-2.0
- Created: 2024-01-28T02:56:39.000Z (almost 2 years ago)
- Default Branch: module
- Last Pushed: 2024-02-13T00:38:05.000Z (almost 2 years ago)
- Last Synced: 2025-01-15T23:42:41.466Z (11 months ago)
- Topics: ci-cd-pipeline, modules, psgallery, workflow
- Language: PowerShell
- Homepage: https://www.powershellgallery.com/packages/WindowsPath
- Size: 36.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# **The WindowsPath Type**
 
[](https://github.com/sangafabrice/windows-path-type/actions/workflows/publish-module.yaml)
Author: Fabrice Sanga
The `WindowsPath` type performs operations on string instances that contain valid Windows File System path information. The specified path or file name string may point to a nonexistent file system object that is nonetheless valid and may carry information about locally or network-shared files and directories.
The module also provides two validation attributes `ValidateWindowsPath` and `ValidateWindowsFileName`.
The most probable use case is to ensure that file name and path strings are validated before being used in a method or property. This can be useful for instance when validation of the file system object occurs before its creation.
[](https://www.powershellgallery.com/packages/WindowsPath)
## Constructor
|||
|:---|---|
|[WindowsPath(String)]()|Initializes a new instance of the WindowsPath class on the specified path string.|
## Property
|||
|-|-|
|[Path]()|Represents the validated fully qualified path string.|
## Method
|||
|-|-|
|[GetFullPath(String)]()|Class method that returns the and absolute and simplified version of the specified path string when it is valid by removing unnecessary characters. Otherwise, it returns a null-value.|
## Use cases
### Example 1:
The `PSProvider` is a shortcut to a subdirectory in the file system hierarchy, such as `Temp` that expands to the `$Env:TEMP` folder path.
```powershell
PS> using module WindowsPath
PS> Get-PSProvider FileSystem | Select-Object -ExpandProperty Drives | Where-Object Name -iLike ??* | Select-Object Name,Root
Name Root CurrentLocation
---- ---- ---------------
Temp C:\Users\username\AppData\Local\Temp\ NewFolder\NewA\NewB
PS> [WindowsPath] "Temp:\sub\e1f8a7c2.tmp"
Path
----
C:\Users\username\AppData\Local\Temp\sub\e1f8a7c2.tmp
PS> [WindowsPath] "Temp:sub\e1f8a7c2.tmp"
Path
----
C:\Users\username\AppData\Local\Temp\NewFolder\NewA\NewB\sub\e1f8a7c2.tmp
PS> Set-Location Temp:
PS> $PWD.Path
Temp:\NewFolder\NewA\NewB
PS> [WindowsPath] "\sub\e1f8a7c2.tmp"
Path
----
C:\Users\username\AppData\Local\Temp\sub\e1f8a7c2.tmp
PS> [WindowsPath] "sub\e1f8a7c2.tmp"
Path
----
C:\Users\username\AppData\Local\Temp\NewFolder\NewA\NewB\sub\e1f8a7c2.tmp
PS> Get-Item @('Temp:\sub\e1f8a7c23.tmp','Temp:\NewFolder\NewA\NewB\sub\e1f8a7c2.tmp')
Get-Item: Cannot find path 'Temp:\sub\e1f8a7c23.tmp' because it does not exist.
Get-Item: Cannot find path 'Temp:\NewFolder\NewA\NewB\sub\e1f8a7c2.tmp' because it does not exist.
```
### Example 2:
The file system provider or drive does not exist.
```powershell
PS> using module WindowsPath
PS> @(Get-PsProvider FileSystem | Select-Object -ExpandProperty Drives | Where-Object Name -IEQ A).Count
0
PS> Test-Path "B:sub\e1f8a7c2.tmp" -IsValid
False
PS> [WindowsPath]::new("B:sub\e1f8a7c2.tmp")
Exception: The string "B:sub\e1f8a7c2.tmp" is not a valid Windows path string.
PS> [WindowsPath]::new("B:\sub\e1f8a7c2.tmp")
Exception: The string "B:\sub\e1f8a7c2.tmp" is not a valid Windows path string.
```
### Example 3:
The `PSProvider` is a network share. The IPv4 address of the server of the share is `192.168.0.5` and its is `nwshare`.
```powershell
PS> using module WindowsPath
PS> Get-SmbShare | Where-Object Name -INotLike '*$' | Select-Object Name,Path
Name Path
---- ----
test C:\Batch\testdir
PS> [WindowsPath] "\\$Env:COMPUTERNAME\test\sub\e1f8a7c2.tmp"
Path
----
\\nwshare\test\sub\e1f8a7c2.tmp
PS> [WindowsPath] "\\192.168.0.5\test\sub\e1f8a7c2.tmp"
Path
----
\\192.168.0.5\test\sub\e1f8a7c2.tmp
PS> Set-Location '\\nwshare\test\dist'
PS> $PWD.Path
Microsoft.PowerShell.Core\FileSystem::\\nwshare\test\dist
PS> [WindowsPath] "\sub\e1f8a7c2.tmp"
Path
----
\\nwshare\test\sub\e1f8a7c2.tmp
```
Note that IPv6 is also supported.
### Example 4:
The network does not exist.
```powershell
PS> using module WindowsPath
PS> Test-Path "\\server\test\"
False
PS> Test-Path "\\192.168.0.10\test\"
False
PS> Test-Path "\\nwshare\xtest\"
False
PS> $PWD.Path
C:\Batch\testdir
Name Path
---- ----
test C:\Batch\testdir
PS> [WindowsPath] "\\server\test\sub\e1f8a7c2.tmp"
Path
----
C:\server\test\sub\e1f8a7c2.tmp
PS> [WindowsPath] "\\192.168.0.10\test\sub\e1f8a7c2.tmp"
Path
----
C:\192.168.0.10\test\sub\e1f8a7c2.tmp
PS> [WindowsPath] "\\nwshare\xtest\sub\e1f8a7c2.tmp"
Path
----
C:\nwshare\xtest\sub\e1f8a7c2.tmp
```
### Example 5:
The path contains invalid characters.
```powershell
PS> using module WindowsPath
PS> [WindowsPath]::new("C:\Batch\test\sub\e1f8a7c2.tmp")
Path
----
C:\Batch\test\sub\e1f8a7c2.tmp
PS> # There is a whitespace at the end of a path segment (sub).
PS> [WindowsPath]::new("C:\Batch\test\sub \e1f8a7c2.tmp")
Exception: The string "C:\Batch\test\sub \e1f8a7c2.tmp" is not a valid Windows path string.
PS> # The file path string has a wildcard character.
PS> [WindowsPath]::new("C:\Batch\test\sub\e1f8a7c2?.tmp")
Exception: The string "C:\Batch\test\sub\e1f8a7c2?.tmp" is not a valid Windows path string.
PS> # The file path string has a > sign.
PS> [WindowsPath]::new("C:\Batch\te>st\sub\e1f8a7c2.tmp")
Exception: The string "C:\Batch\te>st\sub\e1f8a7c2.tmp" is not a valid Windows path string.
```
Compare the result to `Test-Path` bound with the `IsValid` parameter.
```powershell
PS> Test-Path "C:\Batch\test\sub \e1f8a7c2.tmp" -IsValid
True
PS> Test-Path "C:\Batch\test\sub\e1f8a7c2?.tmp" -IsValid
True
PS> Test-Path "C:\Batch\test\sub>\e1f8a7c2.tmp" -IsValid
True
```