powershell iterate all users and display recent lnk files - powershell

Iterate all windows $users and display the recent .lnk files from a specific path!
I have tried importing this module - https://gist.github.com/picheljitsu/cc2ed99cbae7caad3abb0928cd8a286b
Get-RecentFiles and I want to iterate with $users after getting the users with get-localuser
$user = (Get-LocalUser | Select-Object Name) |
ForEach-Object { Get-RecentFiles $user }
should display recent files of all users recent directory..
Directory: C:\Users\admin\AppData\Roaming\Microsoft\Windows\Recent
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 6/30/2019 6:59 PM AutomaticDestinations
d----- 7/1/2019 3:21 PM CustomDestinations
Directory: C:\Users\user2\AppData\Roaming\Microsoft\Windows\Recent
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 6/30/2019 6:59 PM AutomaticDestinations
d----- 7/1/2019 3:21 PM CustomDestinations

The result of Get-LocalUser | Select-Object Name is an array of users. When you pass this array to the pipeline, it will "unwrap" its items and pass them one at a time, and this item will be declared as $_ variable.
Passing Arrays to Pipeline
If a function returns more than one value, PowerShell wraps them in an array. However, if you pass the results to another function inside a pipeline, the pipeline automatically "unwraps" the array and processes one array element at a time.
ExpandProperty parameter is used to convert the object property Name to string to be used in the Get-RecentFiles function.
Modify your code and try this:
Get-LocalUser | Select-Object -ExpandProperty Name | Foreach-Object {Get-RecentFiles $_}
Update
The above code will get some errors for the disabled users (e.g: administrator, guest). To solve this, you have to only get the enabled users as follows:
Get-LocalUser | Where-Object Enabled | Select-Object -ExpandProperty Name | Foreach-Object {Get-RecentFiles $_}

Related

Powershell: How To Extract Binary File Attributes From Directory Listing

In PowerShell, I can get a nice list of files in descending sorted order using a filter:
$tt = gci -Path \\Munis2\musys_read\export_test\* -Include "ARLMA_*.csv" | sort LastWriteTime -Descending
PS H:\WindowsPowerShell\Scripts\ProductionScripts\Munis> $tt
Directory: \\Munis2\musys_read\export_test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 03/04/2022 3:09 AM 25545520 ARLMA_20220304030027.csv
.
.
.
Then, I can get just the name of the file for the purposes of transferring that file to an FTP site.
PS H:\WindowsPowerShell\Scripts\ProductionScripts\Munis> $tt[0].Name
ARLMA_20220304030027.csv
How can I parse $tt[0].LastWriteTime
PS H:\WindowsPowerShell\Scripts\ProductionScripts\Munis> $tt[0].LastWriteTime
Friday, March 4, 2022 3:09:14 AM
into something that looks like yymmddhhmmss, or is there a way to get the binary time of the file the last time it was accessed?
The ToString() method can be used to format the date into a string. Are you sure that a two digit year is appropriate?
$DateResult = (Get-ChildItem -Path \\Munis2\musys_read\export_test\* -Include "ARLMA_*.csv" |
Sort-Object -Property LastWriteTime -Descending |
Select-Object -First 1).LastWriteTime.ToString('yyMMddHHmmss')

powershell select-object property AND expandproperty

Trying to execute a Select-Object, but i can't seem to return property and a value from a key-value list property. There are 3 properties i want returned RunStart, Message and 'tableName' from this Parameters property.
I get too much with this:
|Select-Object -Property Parameters,RunStart,Message
Parameters RunStart Message
---------- -------- -------
{[tableName, AHROR012], [schemaName, dbo]...} 11/14/2019 5:39:06 PM Operation on target failed
But I dont get the RunStart or Message when i do this:
|Select-Object -ExpandProperty Parameters -Property tableName,RunStart,Message
Key Value
--- -----
tableName AHROR012
How do i do it to get:
Parameters.tableName RunStart Message
---------- -------- -------
AHROR012 11/14/2019 5:39:06 PM Operation on target failed
THANKS!!!
... | Select-Object -Property #{
Name='ParametersTableName';
Expression={ $_.Parameters.tableName }
}, RunStart, Message
A more elegant solution would be to use a 2nd select statement:
... | select RunStart,Message -ExpandProperty Parameters | Select RunStart,Message,tableName

Get-ADComputer from Multiple Computers from a CSVFile

I have a csv file full of computer information formatted:
Name OS Site Code AD_Status Region Tech
computerone Windows 10 Enterprise **** Exists Chicago T T
computertwo Windows 10 Enterprise **** Exists Chicago T T
computerthree Windows 10 Enterprise **** Exists Chicago T T
I'm running a Powershell script that grabs the computer name from the csv file and checks its 'modifyTimeStamp' field.
$csvfile = Import-CSV -Path 'C:\Users\****\testexcel.csv'-Delimiter ","
$numofcompsincsv = $csvfile.psobject.properties.value[0] - 1
for ($i = 0; $i -le $numofcompsincsv; $i++) {
Get-ADComputer -identity $csvfile[$i].psobject.properties.value[0] -Properties * | FT Name, modifyTimeStamp
}
The problem with this is that it prints the computer information one by one, for example:
Name modifyTimeStamp
---- ---------------
computerone 7/19/2019 11:06:22 AM
Name modifyTimeStamp
---- ---------------
computertwo 7/24/2019 6:02:14 AM
Name modifyTimeStamp
---- ---------------
computerthree 7/24/2019 2:02:14 AM
How can I modify this so that it prints all in one like:
Name modifyTimeStamp
---- ---------------
computerone 7/19/2019 11:06:22 AM
computertwo 7/24/2019 6:02:14 AM
computerthree 7/24/2019 2:02:14 AM
Don't do this with a for loop; use the pipe instead:
Import-CSV -Path 'C:\Users\****\testexcel.csv' | Select -expand Name | Get-ADComputer -prop modifyTimeStamp | Select Name,ModifyTimeStamp
ETA: I apparently didn't create the CSV (hand-coded) I used for testing quite properly, and had originally piped the CSV to | Select Name | instead of expanding the property (which worked in my test). When I re-did it using Excel, I verified that | Select -expand Name | was required, per the comment by #js2010.

Get-ChildItem -Path in NLog file

If I have this:
Get-ChildItem -Path $BACKUP_REG_PATH >> $TOT_LOG_FILE
I will get a fine list in my log file like this:
Directory: C:\WS\BACKUP\xxxx-Reg
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2016-05-17 11:04 494018 xxxxxx_REGISTRY_EVENTLOG__2016-05-17__11_04_38.reg
-a--- 2016-05-17 11:08 494018 xxxxxx_REGISTRY_EVENTLOG__2016-05-17__11_08_59.reg
-a--- 2016-05-17 11:10 494018 xxxxx_REGISTRY_EVENTLOG__2016-05-17__11_10_31.reg
I want to do this for NLog instead but I don't know how to get a nice list as above.
If I do this:
$regtxt=Get-ChildItem -Path $BACKUP_REG_PATH
$LOGGER.Trace("$regtxt");
I only get a long list on the same row with the Name column.
Any ideas how to solve this?
I don't know NLog but the Trace method probably output the trace in a single line. You could iterate over each item using the Foreach-Object cmdlet and write a trace:
Get-ChildItem -Path $BACKUP_REG_PATH | Foreach-Object {
$LOGGER.Trace($_);
}
Note: This will not output the name column, you may have to trace this yourself.
To solve this, you could pipe the output to the Out-String cmdlet which will give you a single string. You then have to split the string by [System.Environment]::NewLine to get an array to iterate over it:
((Get-ChildItem | select -first 4 | Out-String) -split [System.Environment]::NewLine) |
ForEach-Object {
$LOGGER.Trace($_);
}

Extracting text from output

I am creating my first application with VS and PowerShell and need to get the name of a service from my listview. The output of the selection looks like this:
ComputerName Status Name DisplayName
------------ ------ ---- -----------
PC Running Appinfo Application Information
What I want to do is get the value Appinfo from the Name column and assign it to a variable. I've had no luck with regex, but then again I am a beginner so I could be doing something wrong. Is there an easy way to do this?
The output you're currently getting is a formatted (tabular) view on selected properties of an object (namely the properties ComputerName, Status, Name, and DisplayName). You can get the value of a particular property by expanding it via the Select-Object cmdlet:
$name = ... | Select-Object -Expand Name
You could also store the object in a variable and access the property via dot-notation:
$obj = ...
$name = $obj.Name
Beware that if a cmdlet outputs multiple objects the variable will contain an array:
PS C:\> $services = Get-Service
PS C:\> $services.GetType().FullName
System.Object[]
PS C:\> $services.Count
136
You can access properties of a member object by index:
$services[42].Name
or by selecting a specific object via its properties:
$services | Where-Object { $_.DisplayName -eq 'Application Information' } |
Select-Object -Expand Name
Since PowerShell v3 you can also use the dot-property notation to get the value of a particular property of all array members:
$services.Name
Prior to PowerShell v3 this would have thrown an error, because the array object doesn't have a property Name.