Need help with Powershell Command


I am new with powershell and I need some help with command.

Here is my data:

Diagnostic data directory path (DIAGPATH) = c:\users\db2admin\

What I am want to get the value after the = sign, the output should look like this

Here is the command I running to get the data, but I can’t figure out how to get c:\users\db2admin\

db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match “ALT_DIAGPATH”)}

If ther an option to add to above command to print last value after = sign?

Hello, you can use the Split method of System.String, ie:

(db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match “ALT_DIAGPATH”)}).Split("=")[1]

and in your case even

(db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match “ALT_DIAGPATH”)}).Split("=")[1].Trim()
^ ^
to get rid of the whitespace (Note the parentheses)

I always feel awkward specifying the array index like that, but if you know there’s only one split character, it seems straightforward, maybe someone has a more elegant approach.

Hi Greg,

Thank you for reply. I am getting the following error while excuting the command.

PS C:\Users\db2admin> (db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match “ALT_DIAGPATH”)}).Split("=")[
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn’t contain a method named ‘Split’.
At line:1 char:93

  • (db2 get dbm cfg | select-string -Pattern DIAGPATH|? {-not($_ -match “ALT_DIAGPATH”)}).Split <<<< ("=")[1].Trim()
    • CategoryInfo : InvalidOperation: (Split:String) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

Hi Hamid,

I was actually expecting to see a post of Dave’s, since he is a Regex expert :slight_smile:
But maybe I can help you too:

$a = "Diagnostic data directory path (DIAGPATH) = c:\users\db2admin\" $null = $a -match "(?<== ).*" $Matches.Values

Explanation: first we assign the string to a variable.
Then we do a Regex match: find all characters after "= ". We assign the match to the $null variable, just to avoid the output “True” which tells you that there is a valid match.
At last point, we just call the found matches.

Hope this helps!

Hi deiandrei and Greg,

Thank you for both of suggestion. I found a solution, it seems very easy:

((db2 get dbm cfg | select-string DIAGPATH) -split ’ = ')[1]