Opening the output file automatically - powershell

I have a script that creates a csv file which I then go open to get the information I am looking for. Is there a way to add to the script so that the file opens immediately as well as saves in the specified location?
Get-ADComputer -Filter { Enabled -eq $true } -Properties Name -SearchBase
"DC=REMOVED,DC=com" |
Select Name |
? {$_. Name -like "X*" } |
Export-Csv "C:\scripts\Computers.csv"

Add this quick snippet to the end of your code:
Invoke-Item "C:\scripts\Computers.csv"
If you have Excel automatically configured to support/open .csv files, it should open automatically.
EDIT: Note that this isn't available in Powershell 2.0 - only 3.0+

Related

How to get the get-ADPrincipalGroupMembership for all users in a txt or csv file and put into a txt file for each user?

I am trying to get a file with the group-memberships for every user that is specified in a txt/csv file.
so this is what i had before:
Get-ADPrincipalGroupMembership -Identity $user -Server $DC | Select name | Where-Object name -like GUSR_* | Out-File "C:\temp\$user.txt"
this work fine for getting the groups from 1 singel user, but now i have to do this for 100+ users.
And instead of doing it one by one i am looking for a way to automate it.
so i got myself a .csv export of all the users i want this done for.
and started trying.
what i came up with so far:
$users = Get-Content "C:\temp\test.csv" |ForEach-Object {Get-ADPrincipalGroupMembership -Identity $users -Server $DC | Select name | Where-Object name -like GUSR_* | Out-File "\\ads.net\ADS\SDL\Temp\_ROLAND\RSD\test2\$users.txt"}
This cleary doesnt work.
I have tried a couple of other things with the foreach command but nothing did the trick.
I have the feeling i am not on the right path to get my result.
Maby somebody has done this before and can help me get on the right path.
i'm not new to powershell but i'm far from an expert, most of the time i use it for basic singel commands or edit some great scripts i find.
sadly for this i haven't found any yet.
with kind regards
Roland
Don't assign back to a variable
Import the CSV
No filter after select
Pretiffy your -like
Use $_ as pipeline variable
Use subexpression operator for string+variable concatenation
Import-Csv "C:\temp\test.csv" |ForEach-Object {Get-ADPrincipalGroupMembership -Identity $_.users -Server $_.DC | Where-Object {$_.name -like 'GUSR_*'} | Select -Expand Name | Out-String | Out-File "\\ads.net\ADS\SDL\Temp\_ROLAND\RSD\test2\$($_.users).txt"}

PowerShell script bigger KB file then if I open the file and save it

PowerShell script produces a 275KB csv file that will not run in a program we use here but if I open the file and do nothing to it but save it, it will save around 238KB and the program runs fine at the point is there a way I can run a program to open the file and save it after the script runs each night or has maybe there is a way to export csv file that won't cause the difference. here is my script below
$OUs = "OU=Windows 10 Users,OU=XXX Users,DC=XXXXXXXX,DC=XXXXXXXXX,DC=com","OU=Domain Users,OU=XXX Users,DC=XXXXXXX,DC=XXXXXx,DC=com"
$result =
foreach($OU in $OUs){
Get-ADUser -Filter * -SearchBase $OU -Properties UseraccountControl, displayName, sAMAccountName, extensionAttribute1, extensionAttribute3, mail |
where {$_.UseraccountControl -notlike “*accountdisabled*”} |
Select-Object displayName, sAMAccountName, extensionAttribute1, extensionAttribute3, mail}
$result |Export-Csv C:\Temp\test2.csv -NoTypeInformation

Powershell results different based on save location

I am using powershell to extract all users from an OU who have not signed into their account in 365 number of days.
import-module activedirectory
get-aduser -SearchBase 'ou=staff,ou=brummitt,dc=DUNELAND,dc=LOCAL' -filter 'enabled -eq $true' -Properties samaccountname,lastlogondate |
Where-object {$_.lastlogondate -lt (get-date).AddDays(-365)} |
Select-Object -ExpandProperty samaccountname >>'C:\stale\brummitt.txt'
In attempt to organize the folder these are stored in I have created a folder in my servers C: drive called stale and have a folder called scripts in which the powershell scripts are stored.
When I run the script with powershell and the save extension is C:\stale\brummitt.txt it outputs all users in that OU. When the save location is C:\brummitt.txt it returns the correct users who have not signed in for over a year. Why would the results be changing based on the save location and how can this be combated?
Added:
I am running the powershell script from within the scripts folder.
Did you try using Tee-Object as a part of the pipeline?, that will give you the opotunity to check the stream to the file on console,

Powershell Script to Display Manager Name by Full Name

I have a script that creates a csv file that tells me users who haven't logged in in 90 days. It needs to include the Manager for that user but when pulling in the manager from Active Directory, I"m getting the full DN for that manager rather than just the Display Name. Here's my script...
import-module activedirectory
get-aduser -filter * -searchscope subtree -searchbase "ou=user departments ou,dc=acr,dc=org" -properties DisplayName,manager,lastlogontimestamp |
? {(((Get-date) - ([datetime]::FromFileTime($_.lastlogontimestamp))).TotalDays -gt 90)} |
select DisplayName,samaccountname,manager,Userprincipalname,#{Exp={([datetime]::FromFileTime($_.lastlogontimestamp))};label="Last logon time stamp"} |
export-csv "c:\scripts\reston_sharepoint_users_120_days.csv" -NoTypeInformation -Delimiter ","
Any suggestions? The rest of the script works perfectly. Thanks.

powershell - empty list of group's members

i'm trying to export to csv file list (or table but for now it doesn't matter) with the members of a group .
after many searches and i tried :
Get-ADGroupMember -Identity **genericGroupName** -recursive | select name | Export-csv output.csv -NoTypeInformation
i also tried to change some things in this command- remove the -recursive .
i tried to find the problem in the command and remove the -Export-csv output.csv -NoTypeInformation
but it showed nothing (in nothing i mean - show my command and then nothing)
my problem is that i got the output.csv , but it is empty.
if it matters :
the genericGroupName is actually :
$group = (Get-ADUser -Identity userName -properties MemberOf | select memberOf).memberOf
and
genericGroupName --> $group[0]
this command works well.
I fixed this problem (accidentally) by closing the powershell ISE and then open it.
I am still don't know what was the problem but somehow it fixed by closing the powershell