#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo:Use an Assertion to Validate/Create a Folder

Be sure to check out my Scripting4Crypto initiative. It’s a fun way to get into using cryptocurrencies all while getting your PowerShell needs met.

In this post I will demonstrate how to use an Assertion to verify if a ProjectWise folder exists in the current datasource. If the folder does not exist, it will be created. This assumes the user running the script has permissions to create the folder. This is just a quick demonstration to yet another powerful PowerShell tool.

Click the following link to learn more about Assertions. Wiki – Assertions

We will be using the following cmdlets to accomplish this task. All of the ProjectWise related cmdlets are available using the PWPS_DAB module. At the time of this post, I am using version Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.

  • Get-PWFolders
  • New-PWFolder

Create Assert Definition

Here will create the Assert definition. It will need to be run prior to using.

Basically, the Assert-PWFolderExists will test to determine if the provided folder exists within the current ProjectWise datasource. If it does, a message will be returned. If it does not, a message will be returned letting you know it does not exist and will attempt to create it.

filter Assert-PWFolderExists {
    if( -not (Get-PWFolders -FolderPath $_ -JustOne)){
        Write-Warning "'$_' does not exist in the current datasource. Attempting to create."
        try {
            $null = New-PWFolder -FolderPath $_
            Write-Host "Successfully created folder '$_'." -ForegroundColor Green
        } catch {
            Write-Warning -Message $Error[0].Exception.Message
    } else {
        Write-Host "'$_' exists within the current datasource." -ForegroundColor Green
} # end filter Assert-PWFolderExists

Use Assertion

Now lets give it a try and see what it does.

# Variable containing folder path and name of folder to be validated.
$pwFolder = 'Projects\TestProject'

# Pipe the variable to the Assertion.
$pwFolder | Assert-PWFolderExists

The following shows the returned message when the folder exists.


The following shows when the folder does not exist and is successfully created.


The following shows when the folder does not exist and fails to create it. In this case, the reason is the  user does not have permissions to create the folder. You can use the Get-PWError cmdlet to get the description for a returned error id.


Experiment with it and have fun. And please, let me know if there is a topic you would like to see a post for. Or share a solution you have developed.

Hopefully, you find this useful. Please let me know if you have any questions or comments.  If you like this post, please click the Like button at the bottom of the page. And thank you for checking out my blog.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.