How do I find all .git\config files? - regex

I'm trying to find all .git\config files but I cannot figure out how to do it.
When I just use \.git as a pattern, it finds all directories
Get-ChildItem -Path C:\Repositories -Recurse -Hidden |
Where-Object {
$_.FullName -match '\.git'
} |
Select-Object FullName
but when I exapnd it to \.git\\config$ to give me only the config files it yields no results:
Get-ChildItem -Path C:\Repositories -Recurse -Hidden |
Where-Object {
$_.FullName -match '\.git\\config$'
} |
Select-Object FullName
What am I missing here? Is it because the config file does not have an extension?
I'm using powershell 5.1.

config isn't a hidden file, so only showing hidden files is causing it to be ignored.
Get-ChildItem -Recurse -Force | where-object fullname -match '\.git\\config'

Related

Powershell get list of sub folders and number of files in each in a table format

I'd like to create a table showing the number of files in all subfolders named "andre tegninger"
I've found the following code on here which I modified to filter by the sub folder I am interested in.
dir -filter "*Andre tegninger*" -recurse | ?{ $_.PSIsContainer } | %{ Write-Host $_.FullName (dir $_.FullName | Measure-Object).Count }
This works fine but is there a way I can get name of folder and number of files in two separate columns?
Thank you!
You can use Select-Object to get in separate fields the expected properties :
dir -filter "*Andre tegninger*" -recurse | ?{ $_.PSIsContainer } | Select-Object -Property FullName, {(dir $_.FullName | Measure-Object).Count}
Since -Property expect a property (obviously) You have to use {} around (dir $_.FullName | Measure-Object).Count to tell powershell it's a calculated expression.
You're getting as output :
FullName (dir $_.FullName | Measure-Object).Count
-------- --------------------------------------
Your list of folders...
You can label the fields names to get them some fancy names, such as :
dir -filter "*Andre tegninger*" -recurse | ?{ $_.PSIsContainer } | Select-Object -Property #{label="Path";expression={$_.FullName}}, #{label="Numer of files";expression={(dir $_.FullName | Measure-Object).Count}}
And you get as output :
Path Numer of files
---- --------------

get content from a list and run the Get-ADOrganizationalUnit for that list

I want to run the following script:
Get-ADOrganizationalUnit -Filter "Name -like `"*$OUs*`"" |
Select-Object DistinguishedName, Name
But I want to pass in a list of values for $OU from c:\temp\list
$OUs=c:\temp\list
foreach ($OU in OUs) {
Get-ADOrganizationalUnit -Filter "Name -like `"*$OUs*`"" |
Select-Object DistinguishedName, Name |
Export-CSV -Path c:\temp\list.csv
}
or something like that.
Your existing code should work, given some typos are corrected:
$OUs = Get-Content -Path 'C:\Temp\list.txt'
foreach ($OU in $OUs) {
Get-ADOrganizationalUnit -Filter "Name -like '*$OU*'" |
Select-Object -Property DistinguishedName, Name |
Export-Csv -Path 'C:\Temp\list.csv' -NoTypeInformation -Append -Force
}

Extract MAC address and UUID from string

I extract string containing a lot of text and both MAC address and UUID.
For example:
![LOG[AA:AA:AA:AA:AA:AA, 0A0A0000-0000-0000-0000-A0A00A000000: found optional advertisement C0420054]LOG]!><time="09:07:57.573-120" date="04-19-2017" component="SMSPXE" context="" type="1" thread="2900" file="database.cpp:533"
I would like to strip the output to only display the MAC Address (e.g AA:AA:AA:AA:AA:AA) and UUID (e.g 0A0A0000-0000-0000-0000-A0A00A000000)
I donĀ“t know how to trim the output.
Here is my script:
$Path = "\\AAAAAAAA\logs$"
$Text = "AA:AA:AA:AA:AA:AA"
$PathArray = #()
$Results = "C:\temp\test.txt"
# This code snippet gets all the files in $Path that end in ".txt".
Get-ChildItem $Path -Filter "*.log" |
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
$PathArray += $_.FullName
}
}
Write-Host "Contents of ArrayPath:"
$PathArray | ForEach-Object {$_}
get-content $PathArray -ReadCount 1000 |
foreach { $_ -match $Text}
Instead of using the Where-Object cmdlet to filter all files, you can use the -Filter switch of the Get-ChildItem cmdlet. Also you don't have to load the content using the Get-content cmdlet yourself, just pipe the files to the Select-String cmdlet.
To grab MAC, UUID I just googled both regex and combined them:
$Path = "\\AAAAAAAA\logs$"
$Pattern = '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}),\s+(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})'
$Results = "C:\temp\test.txt"
Get-ChildItem $Path -Filter "*.log" -File |
Select-String $Pattern |
ForEach-Object {
$_.Matches.Value
} |
Out-File $Results

match multi-line string

I am using a PowerShell command to find all *.vue files (it's a simple text format) in a directory, where I need to match this:
7,Id
6,Default
So, these are 2 consecutive lines. With Notepad++ I see CRLF at the end of the line. Following Google searches, this must be close:
Get-ChildItem "D:\Wim\TM1\TI processes" -Filter *.vue -Recurse |
Select-String -Pattern "7,Id\r\n6,Default" -CaseSensitive |
Out-File C:\test.txt
But it does not find the files. I checked that I can find the first part (7,Id) correctly, and also the second part (6,Default), but the combination with the newline is not working.
Any ideas please? Maybe an alternative?
I can have a workaround but it's inefficient and a lot of coding. For example, I could use PowerShell to provide a list of only the first sentence, then process these files to see if it matches the second sentence as well. I want to avoid that.
You need to pass the content of the file as a single string, otherwise Select-String will apply the pattern to each line separately.
Get-ChildItem "D:\Wim\TM1\TI processes" -Filter *.vue -Recurse | ForEach-Object {
Get-Content $_.FullName | Out-String |
Select-String -Pattern "7,Id\r\n6,Default" -CaseSensitive |
Select-Object -Expand Matches |
Select-Object -Expand Groups |
Select-Object -Expand Value
} | Out-File C:\test.txt
On PowerShell v3 and newer you can use Get-Content -Raw instead of Get-Content | Out-String.
As an alternative to Select-String you could use the -cmatch operator in a Where-Object filter:
Get-ChildItem "D:\Wim\TM1\TI processes" -Filter *.vue -Recurse | ForEach-Object {
Get-Content $_.FullName | Out-String | Where-Object {
$_ -cmatch "7,Id\r\n6,Default"
} | ForEach-Object {
$matches[0]
}
} | Out-File C:\test.txt
With Select-String, the -Pattern parameter is regex capable, so try this:
Get-ChildItem "D:\Wim\TM1\TI processes" -Filter *.vue -Recurse |
Select-String -Pattern "7,Id|6,Default" -CaseSensitive |
Out-File C:\test.txt
The vertical pipe bar (|) acts as an alternative separator, or in otherwords, an "or" operator. With the pattern it will match either.

Finding directories with a certain regex

I think the regex is right for powershell, but I think my logic is wrong.
What I want to do is get a list of all the directories that start with 4-to-6 digits. What I get so far is the child items in directories that start with 4-6 digits:
get-childitem -path \\server\share -recurse |
where { ($_.psiscontainer) -and ($_.name -match "^\d{4,6}") }
Can I somehow pipe into a write for 'current object' rather than child?
So in the end I just piped into | ft, making the total command:
get-childitem -path \\path\path -recurse | where {($_.psiscontainer)
-and ($_.name -match "^\d{4,6}" )} | Select-Object Name, FullName | ft